首页 > 解决方案 > SignalRCore 和 Azure 函数

问题描述

我有一个 Asp.Net-Core 3.0 Web 服务,我将其作为单一容器功能应用程序运行。这对我的 Rest api 非常有用。

但是我最近添加了一个 SignalR 集线器来添加通知服务,但这在 Azure 函数中我的 Web 应用程序的托管版本中不起作用。当我尝试使用 .Net SignalRCore 3 客户端连接到集线器时,出现以下错误:

服务器在握手开始之前断开了连接。

当我将容器作为基本 Azure 容器实例运行时,SignalR 功能运行良好。

有什么想法为什么会发生这种情况,甚至可以将 SignalR 集线器添加到托管的 azure 函数 docker 容器中吗?

标签: azuresignalrazure-functionsasp.net-core-signalr

解决方案


似乎 SignalR 在设计上托管在函数应用程序中的 Asp.Net-Core3 Docker 容器实例中不起作用,因为函数应用程序应该是无状态的,而 SignalR 不是。为此,您需要使用 AspNetCore Web 服务中的单独 Azure SignalR 服务。

https://azure.microsoft.com/en-us/services/signalr-service/

StartUp并更改您班级的注册码。

var signalRServerBuilder = services.AddSignalR();

var signalRConnectionString = m_configuration[@"SignalRConnectionString"];
if (!string.IsNullOrWhiteSpace(signalRConnectionString))
{
    signalRServerBuilder.AddAzureSignalR(signalRConnectionString);
}

推荐阅读