gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:22:46 25 4
gpt4 key购买 nike

我正在尝试使用隔离进程在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com