【问题标题】:Azure SignalRTrigger not working with Azure FunctionsAzure SignalRTrigger 不适用于 Azure Functions
【发布时间】:2022-11-15 15:07:49
【问题描述】:

我使用 Azure Functions 创建了 Azure SignalR Serverless 服务。 我的客户端是一个 .NET 6 WPF 应用程序。

协商功能按预期工作,连接成功建立。 CosmosDBTriggerHttpTriggerTimerTrigger 函数也可以按预期工作。 但是,SignalRTrigger 不起作用,我不知道为什么。

SignalRTrigger 函数:

[FunctionName("SignalRTest")]
public async Task SignalRTest([SignalRTrigger("myHub", "messages", "SignalRTest")] InvocationContext invocationContext, string message, ILogger logger)
{
    logger.LogInformation($"Receive {message} from {invocationContext.ConnectionId}.");
    await Clients.All.SendAsync("signalRTestMessage", message);
}

客户端配置:

connection = new HubConnectionBuilder()
             .WithUrl("https://<SiteURL>.azurewebsites.net/api")
             .Build();
             
await connection.StartAsync().ContinueWith(async (e) =>
{
    try
    {
        await connection.InvokeAsync("SignalRTest", "TestMessage");
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
});

异常总是返回错误信息:

调用失败,状态码 404

我已经使用 Azure Functions 中生成的 signalr_extension 密钥配置了 SignalR 上游。

我已经关注了Microsoft docs 上的官方文档,但仍然无法解决问题。

【问题讨论】:

    标签: c# wpf azure-functions .net-6.0 azure-signalr


    【解决方案1】:

    我很同情你,因为我们也一直在努力发送消息,尽管如此多的文档使它看起来微不足道。我对您的情况的一个想法是:您是否应该在创建 hubConnection 时使用协商响应中的 URL?

    这是我们的打字稿示例,它使用 NegotiateResponse 对象以及从协商 HTTP 调用返回的 URL 和 AccessToken:

    this.signalRService.negotiate().subscribe({
        next: (negotiateResponse) => {
            let options = {
                accessTokenFactory: () => negotiateResponse.accessToken,
                };
    
            const connection = new signalR.HubConnectionBuilder()
                .withUrl(negotiateResponse.url, options)
                .build();
    

    附言如前所述,我们也在努力发送消息,所以这可能不是你的解决方案,尽管我希望它是。

    【讨论】:

      猜你喜欢
      • 2022-10-07
      • 2022-01-02
      • 1970-01-01
      • 2020-11-17
      • 2018-04-14
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多