Azure 函数中的无服务器 SignalR:对 InvokeAsync 的调用在成功 StartAsync 后挂起

问题描述 投票:0回答:1

我正在尝试使用一个简单的无服务器中心设置 Azure Function,我将用它来通知 Web 客户端。 FunctionApp 包含 serverlesshub(下面称为 MyHub)和客户端(最终将接收 EgentGrid 事件并通过 MyHub 通知客户端)。

客户端成功StartAsync后,调用InvokeAsync时挂起。有什么线索可能是什么问题吗?

using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker.SignalRService;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Net;

new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services =>
    {
        // not sure if this should be used or not. It doesn't seem to make a difference.
        //services.AddSignalR().AddAzureSignalR(); 

        services.AddServerlessHub<MyHub>();
        services.AddSingleton<Client>();
    })
    .Build()
    .Run();

public class Client
{
    private readonly HubConnection _connection;

    public Client()
    {
        string? hostname = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");

        _connection = new HubConnectionBuilder()
            .WithUrl($"http://{hostname}/api")
            .Build();

        _connection.StartAsync().GetAwaiter().GetResult();
    }

    [Function("test")]
    public async Task Negotiate([HttpTrigger] HttpRequestData req)
    {
        await _connection.InvokeAsync("broadcast", req.ToString());
    }
}

[SignalRConnection("AzureSignalRConnectionString")]
public class MyHub : ServerlessHub
{
    private const string HubName = nameof(MyHub);

    public MyHub(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    [Function("negotiate")]
    public static HttpResponseData Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req,
        [SignalRConnectionInfoInput(HubName = HubName)] string connectionInfo)
    {
        HttpResponseData response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "application/json");
        response.WriteString(connectionInfo);
        return response;
    }

    [Function("broadcast")]
    public Task Broadcast(
        [SignalRTrigger(HubName, "messages", "broadcast", "message")] SignalRInvocationContext invocationContext,
        string message)
    {
        return Clients.All.SendAsync(message);
    }
}

注意:如果相关,我将 local.settings.json 设置为:

{
  "Host": {
    "CORS": "http://localhost:5002",
    "CORSCredentials": true
  },
  "IsEncrypted": false,
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.AspNetCore.SignalR": "Trace"
    }
  },
  "Values": {
    "AzureSignalRConnectionString": "Endpoint=https://xxx.service.signalr.net;AccessKey=xxx;Version=1.0;",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  }
}
azure-functions signalr-hub signalr-client signalr-service
1个回答
0
投票

找到了我自己问题的答案。我需要按照https://github.com/aspnet/AzureSignalR-samples/tree/main/samples/BidirectionChat中所述配置上游,但在我的情况下,我不需要提供代码参数,即我使用的 URL 是 https://xxx.ngrok-free.app/runtime/webhooks/signalr.

© www.soinside.com 2019 - 2024. All rights reserved.