Azure SignalR 服务实现 .NET 6 Azure Function 隔离进程

标签 azure azure-functions signalr azure-signalr

我正在尝试使用隔离进程在Azure功能中实现Azure SignalR服务。 到目前为止,它对我来说工作正常。我阅读了 Microsoft 文档,其中提到为了注册任何输出绑定(bind),我需要为其创建 azure 函数。下面是我的代码:

[Function("BroadcastToAll")]
[SignalROutput(HubName = "demohub", ConnectionStringSetting = "AzureSignalRConnectionString")]
public SignalRMessageAction BroadcastToAll([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
    try
    {
        using var bodyReader = new StreamReader(req.Body);
        var d = bodyReader.ReadToEnd();
      return  new SignalRMessageAction("newMessage")
        {
            // broadcast to all the connected clients without specifying any connection, user or group.
            Arguments = new[] { d },
        };
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

我的问题是返回类型必须为 SignalRMessageAction。我怎样才能让它的返回类型像 HttpResponseData.

最佳答案

is it mandatory to have the return type as SignalRMessageAction

返回类型不强制为 SignalRMessageAction

  • 我们可以使用MSDoc中提到的任何返回类型。根据我们的要求。

How can I let its return type like HttpResponseData.

当我们使用 SignalR 创建 Azure Function 时,将使用 HttpResponseData 创建默认函数代码。

enter image description here

我的示例代码:

[Function("negotiate")]
 public HttpResponseData Negotiate(
     [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
     [SignalRConnectionInfoInput(HubName = "HubValue")] MyConnectionInfo 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;
 }
  • 上述默认代码返回HttpResponseData

local.settings.json 文件中添加 SignalRConnectionString。

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "AzureSignalRConnectionString": "Endpoint=https://****.service.signalr.net;AccessKey=****;Version=1.0;"
  }
}

我的.csproj文件:

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.12" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.SignalRService" Version="1.2.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.10.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
  </ItemGroup>

关于Azure SignalR 服务实现 .NET 6 Azure Function 隔离进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76506824/

相关文章:

c# - 如何将消息放入死信并在azure函数中经过一段时间后处理它

azure - 如何从 Bicep 获取 Azure Functions key

c# - 链接时 IAuthenticationHandler 出现错误 XA2006

asp.net-mvc-3 - 用于 asp.net MVC 聊天应用程序的 signalR 与 html5 websockets

ios - IOS 包房聊天要不要用SignalR?

azure - 我可以使用自定义策略将两个电子邮件地址用于 MFA 吗?

azure - 如何在 zsh 中为 Azure CLI 启用命令完成?

azure - 备份 Microsoft Azure 虚拟机

azure - 如何在 kubernetes 中使用 windows 容器挂载卷?

azure - Azure 函数槽是否需要应用程序设置 AzureWebJobsStorage?