You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Functions中SignalRTrigger无法触发问题求助

排查与修复步骤

你的WPF客户端已成功连接Azure SignalR无服务器服务,但调用SignalRTest方法时返回404,SignalRTrigger函数始终未触发,可按以下步骤排查修复:

1. 修正客户端Hub连接URL

客户端代码未指定目标Hub名称myHub,导致SignalR服务无法将请求路由到对应触发器。修改WithUrl参数:

connection = new HubConnectionBuilder()
             .WithUrl("https://<SiteURL>.azurewebsites.net/api/myHub")
             .Build();

无服务器模式下,客户端必须在URL中明确指定Hub名称,否则协商后的连接无法关联到你的myHub,进而找不到对应的触发器函数。

2. 验证上游配置完整性

检查Azure SignalR服务的上游设置,确保:

  • 上游目标URL为:https://<SiteURL>.azurewebsites.net/api/runtime/webhooks/signalr?code=<signalr_extension_key>
  • 上游规则的Hub名称设为myHub,类别选择messages,事件设为SignalRTest(或*匹配所有消息事件)
  • signalr_extension_key是从Azure Functions应用「密钥」页面获取的signalr_extension密钥值,而非AzureSignalRConnectionString。

3. 修正SignalRTrigger函数的消息发送逻辑

你的函数代码直接使用Clients.All.SendAsync是错误的——Azure Functions的SignalRTrigger不支持直接注入IHubContext,需使用SignalR输出绑定发送消息。修改后的代码:

[FunctionName("SignalRTest")]
public async Task SignalRTest(
    [SignalRTrigger("myHub", "messages", "SignalRTest")] InvocationContext invocationContext,
    string message,
    [SignalR(HubName = "myHub")] IAsyncCollector<SignalRMessage> signalRMessages,
    ILogger logger)
{
    logger.LogInformation($"Receive {message} from {invocationContext.ConnectionId}.");
    await signalRMessages.AddAsync(new SignalRMessage
    {
        Target = "signalRTestMessage",
        Arguments = new[] { message }
    });
}

4. 检查方法名大小写一致性

SignalR方法名区分大小写,确保客户端调用的"SignalRTest"与触发器中指定的事件名"SignalRTest"完全一致。

5. 确认函数部署状态

登录Azure门户,检查SignalRTest函数是否成功部署、状态为「启用」,且无部署错误日志。

6. 验证应用设置

确保Azure Functions应用「配置」页面已正确添加AzureSignalRConnectionString,其值为Azure SignalR服务提供的连接字符串。


内容的提问来源于stack exchange,提问作者PhobosFerro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:30:44