如何配置Windows/WSL/Bot Framework Emulator连接WSL中的Bot Framework v4
解决WSL中Bot Framework v4机器人无法被Windows模拟器访问的配置方案
核心问题定位
Bot Framework Emulator默认尝试连接localhost,但WSL的localhost与Windows的localhost存在端点配置差异(即使普通HTTP应用能正常通信),需调整机器人绑定地址和模拟器连接参数。
具体配置步骤
1. 修改WSL中机器人的绑定地址
无论使用C#、TypeScript还是Java开发,都要让机器人绑定到0.0.0.0(而非默认的127.0.0.1),确保Windows侧能访问:
- C#项目:修改
appsettings.json,设置Urls为http://0.0.0.0:3978(默认Bot端口为3978),无需身份验证可留空MicrosoftAppId和MicrosoftAppPassword:
或启动时通过命令行指定:{ "MicrosoftAppId": "", "MicrosoftAppPassword": "", "Urls": "http://0.0.0.0:3978" }dotnet run --urls=http://0.0.0.0:3978 - TypeScript项目:修改
.env文件设置PORT=3978,并在代码中绑定到0.0.0.0,例如index.js中:const server = restify.createServer(); server.listen(process.env.PORT || 3978, '0.0.0.0', () => { ... }); - Java项目:启动时添加参数指定绑定地址和端口,例如:
java -jar your-bot.jar --server.address=0.0.0.0 --server.port=3978
2. 获取WSL的内部IPv4地址
在WSL终端执行以下命令,复制输出的IP地址:
hostname -I | awk '{print $1}'
示例输出:172.17.0.2
3. 配置Bot Framework Emulator连接参数
打开模拟器,点击「Open Bot」,在「Bot URL」中输入WSL IP加Bot端点,例如:http://172.17.0.2:3978/api/messages
- 必须使用WSL实际IP,不能用
localhost - 本地测试无需身份验证时,留空
Microsoft App ID和Password,并勾选「Disable Authentication」
4. 验证Windows防火墙(可选)
若仍无法连接,可临时允许3978端口的入站规则:
- 打开Windows Defender防火墙 → 高级设置 → 入站规则 → 新建规则
- 选择「端口」→ 输入3978 → 允许连接 → 应用到所有网络类型 → 命名规则后保存
额外优化:固定WSL IP(可选)
避免每次重启WSL都重新获取IP,可配置静态IP:
- 在Windows的
C:\Windows\System32\drivers\etc\hosts文件中添加:
其中172.17.0.2 wsl-bot.local172.17.0.2为WSL静态IP,wsl-bot.local为自定义域名 - 在WSL(以Ubuntu为例)中编辑
/etc/netplan/00-installer-config.yaml:
执行network: ethernets: eth0: addresses: [172.17.0.2/20] gateway4: 172.17.0.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] version: 2sudo netplan apply生效,之后可使用http://wsl-bot.local:3978/api/messages连接模拟器
内容的提问来源于stack exchange,提问作者Sławomir Siudek
相关产品推荐
相关产品推荐

