【问题标题】:Azure Functions isolated .Net 6.0 + SignalRAzure Functions 隔离 .Net 6.0 + SignalR
【发布时间】:2023-03-10 08:40:01
【问题描述】:

我的目标是:

  • 在计划函数中 - 向 SignalR 添加消息
  • 在 SPA 应用程序 (vue.js) 中订阅事件并调用 API 以更新视图

目前,我正在尝试在我的 Function 应用程序(隔离,.net 6.0)中向 SignalR 获取任何东西。

我在函数应用中拥有的东西:

    [Function("negotiate")]
    public HttpResponseData Negotiate(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
        [SignalRConnectionInfoInput(HubName = "AdminHub", ConnectionStringSetting = "AzureSignalRConnectionString")] SignalRConnectionInfo connectionInfo)
    {
        _logger.LogInformation($"SignalR Connection URL = '{connectionInfo.Url}'");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString($"Connection URL = '{connectionInfo.Url}'");
        
        return response;
    }
}

    [Function("SendMessage")]
    [SignalROutput(HubName = "AdminHub", ConnectionStringSetting = "AzureSignalRConnectionString")]
    public SignalRMessage SendMessage(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get")] Microsoft.Azure.Functions.Worker.Http.HttpRequestData req)
    {
        return
            new SignalRMessage
            {
                Target = "cancelToHandle",
                MethodName = "cancelToHandle",
                Arguments = new[] { "hello" }
            };
    }

    [Function("SignalRTest")]
    public static async Task SignalRTest([SignalRTrigger("AdminHub", "messages", "cancelToHandle", ConnectionStringSetting = "AzureSignalRConnectionString")] string message, ILogger logger)
    {
        logger.LogInformation($"Receive {message}.");
    }

没有调用协商函数。什么时候调用?

如果我调用 SendMessage,没有错误,但 SignalR 服务没有任何反应。我应该在那里看到连接和消息吗? (目前指标为零)。

我尝试创建一个测试“模拟器”客户端 - 只是一个控制台应用程序:

var url = "http://<azureSignalRUrl>/AdminHub";

var connection = new HubConnectionBuilder()
    .WithUrl(url)
    .WithAutomaticReconnect()
    .Build();

// receive a message from the hub
connection.On<string, string>("cancelToHandle", (user, message) => OnReceiveMessage(user, message));

await connection.StartAsync();

// send a message to the hub
await connection.InvokeAsync("SendMessage", "ConsoleApp", "Message from the console app");

void OnReceiveMessage(string user, string message)
{
    Console.WriteLine($"{user}: {message}");
}

并抛出异常“:'连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机没有响应。(:80)'

我认为我对应该发生的事情缺乏整体理解:

  • 何时触发协商功能
  • 我可以查看我在 Azure 门户(在 SignalR 服务中)发送的消息吗?
  • 如何在测试中轻松接收它们
  • 参数/属性是什么意思(目标/方法名称/类别)。例子: SignalRTriggerAttribute 具有以下构造函数 public SignalRTriggerAttribute(string hubName, string category, string @event, params string[] parameterNames); 并且输出绑定接收我创建的任何自定义模型?
  • 应在 SignalR 服务中设置哪些设置 - 现在我将其设置为无服务器模式 + CORS

【问题讨论】:

    标签: azure azure-functions signalr .net-6.0


    【解决方案1】:

    以下是一些有助于在函数中使用 SignalIr 服务扩展的链接

    使用 SignalIr 服务扩展 https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/Extensions/SignalR/SignalRFunction.cs

    以下是触发协商功能的示例链接 https://github.com/Log234/azure-functions-signalr-dotnet-isolated-demo/blob/main/SignalRDemo/NegotiationFunctions.cs

    要完整了解 SignalIR,请参阅 GithubMS 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      相关资源
      最近更新 更多