SignalR Core 未在 Azure App Service 中使用 Websockets

SignalR Core not using Websockets in Azure App Service

我正在开发使用以下堆栈的 SignalR Core 网络应用程序:

客户端正在使用 SignalR NPM 包 (@aspnet/signalr)。

应用程序配置如下:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy(
            "CorsPolicy",
            builder => builder
                .AllowCredentials()
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader());
    });

    services.AddSignalR();
}

public void Configure(IApplicationBuilder app)
{
    app.UseCors("CorsPolicy");
    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseWebSockets();
    app.UseSignalR(routes =>
    {
        routes.MapHub<ClientHub>("/hubs/notifications");
    });
}

当运行 在http://localhost 本地时,客户端使用WS 协议进行连接。但是当部署在 Azure App Service 中时,它会回退到 SSE。

浏览器日志显示:

WebSocket connection to 'wss://xxxxxx.azurewebsites.net/hubs/notifications?id=ZRniWKpMLMPIyLhS5RSyAg' failed: Error during WebSocket handshake: Unexpected response code: 503
Information: SSE connected to https://xxxxxx.azurewebsites.net/hubs/notifications?id=ig47oOdQzbasdgrlr0cHaw

negotiate 方法似乎 return 支持 Websockets:

{"connectionId":"ZRniWKpMLMPIyLhS5RSyAg",
 "availableTransports":[
  {"transport":"WebSockets","transferFormats":["Text","Binary"]},
  {"transport":"ServerSentEvents","transferFormats":["Text"]},
  {"transport":"LongPolling","transferFormats":["Text","Binary"]}]
}

我错过了什么吗?或者还不支持 WSS?

为了回答我自己的问题,Websocket 连接失败不是 ASP.NET Core 或堆栈的问题,而是由于 Azure App Service 需要在 应用程序设置:

2022年相关答案。

要在现代控制台上启用 Web 套接字,请转至 Home -> App Services -> your_app_name -> (Settings) Configuration -> General Settings 并将 Web sockets 属性 设置为 On