Bot Framework Emulator 连接 /api/messages 时出现 404 错误
结论
问题不在 LuisDialog,而是 Emulator 使用了错误的本地地址。
https://localhost/api/messages 实际会连接 127.0.0.1:443。错误信息:
No connection could be made because the target machine actively refused it 127.0.0.1:443
表示本机 443 端口没有运行这个 Bot。同样,http://localhost/api/messages 默认访问 80 端口,也未必是项目实际监听的端口。
请使用项目启动后浏览器地址栏中的完整地址,包括协议和端口。例如:
http://localhost:3979/api/messages
如果项目确实启用了 HTTPS,则可能是:
https://localhost:44321/api/messages
端口号要以本机的实际运行结果为准,不要直接照抄示例。
为什么会连接失败
项目首页显示的地址:
https://your_bots_hostname/api/messages
只是部署说明,不代表本地项目监听在标准 HTTPS 端口 443。
Visual Studio 和 IIS Express 通常会为项目分配一个端口,例如:
http://localhost:3979/
此时 Bot endpoint 应填写为:
http://localhost:3979/api/messages
协议、主机名和端口都必须与项目实际监听的地址一致。常见配置如下:
| 项目启动地址 | Emulator endpoint |
|---|---|
http://localhost:3979/ | http://localhost:3979/api/messages |
https://localhost:44321/ | https://localhost:44321/api/messages |
http://127.0.0.1:5000/ | http://127.0.0.1:5000/api/messages |
当前异常发生在建立 TCP 连接时,请求还没有到达 Web API。因此,这不是 LUIS 调用失败,也与 Mybot 中的 Intent 方法无关。
正确配置步骤
1. 复制项目的实际启动地址
在 Visual Studio 中启动项目,查看自动打开的浏览器地址栏。假设地址为:
http://localhost:3979/
复制时不要漏掉 :3979。
也可以打开项目属性中的 Web 设置,查看 Project URL 或 IIS Express URL。
2. 在 Emulator 中填写完整 endpoint
将地址设置为:
http://localhost:3979/api/messages
如果浏览器打开的是 HTTPS 地址,就使用相同的 HTTPS 端口:
https://localhost:44321/api/messages
项目只监听 HTTP 时,不要强行改用 HTTPS。HTTP 和 HTTPS 通常使用不同端口,不能只替换协议。
3. 检查 App ID 和密码
本地开发时,如果 web.config 中没有配置 Microsoft App ID 和密码,Emulator 中对应的字段通常也应留空。
如果项目已经配置凭据,Emulator 中的值必须与项目一致。例如:
<appSettings>
<add key="MicrosoftAppId" value="" />
<add key="MicrosoftAppPassword" value="" />
</appSettings>
凭据不匹配通常会触发认证错误,而不是导致 127.0.0.1:443 拒绝连接,但两端的配置仍需保持一致。
确认 /api/messages 控制器存在
Mybot : LuisDialog 只负责定义对话逻辑,不会自行创建 HTTP endpoint。项目还需要一个 Web API 控制器,用于接收 Emulator 发来的 Activity。
典型结构如下:
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
[BotAuthentication]
public class MessagesController : ApiController
{
public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Mybot());
}
return Request.CreateResponse(HttpStatusCode.OK);
}
}
同时还要确认 Web API 路由已经注册。常见配置如下:
using System.Web.Http;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
按照这套路由约定,MessagesController 对应的地址是:
/api/messages
如果控制器已经存在,无需重复创建。
404、405 和连接被拒绝的区别
这些错误对应的问题不同:
Connection refused:协议或端口错误,目标端口没有服务监听。404 Not Found:已连接到服务器,但路由或控制器不存在。405 Method Not Allowed:路由存在,但使用了错误的 HTTP 方法。401 Unauthorized:App ID、密码或认证配置不一致。- HTTPS 证书错误:HTTPS 端口正确,但 Emulator 不信任本地开发证书。
直接在浏览器中访问 /api/messages 时,也可能看到 404 或 405。这是因为浏览器发送的是 GET 请求,而 Bot endpoint 通常只接收 POST 请求。应以 Emulator 发出的请求结果为准。
最终检查示例
如果项目运行后,首页地址是:
http://localhost:3979/
Emulator 应配置为:
Bot URL: http://localhost:3979/api/messages
Microsoft App ID: 留空
Microsoft App Password: 留空
如果此时仍然返回 404,再检查 MessagesController、Web API 路由和控制器名称。
另外,问题中展示的 [LuisModel] 第二个参数是敏感凭据。既然已经公开,应在对应服务中撤销并重新生成。之后请通过配置文件或环境变量读取,不要继续将其硬编码在源码中。
备注:内容仅供参考。
