SignalR Core在Azure App Service中不使用Websockets

Mat*_*nta 2 c# websocket azure-web-sites asp.net-core asp.net-core-signalr

我正在研究使用以下堆栈的SignalR Core Web应用程序:

  • ASP.NET Core 2.1预览2(2.1.0-preview2-final)
  • SignalR Core 1.0预览2(1.0.0-preview2-final)
  • Microsoft.AspNetCore.Cors(2.1.0-preview2-final)
  • Microsoft.AspNetCore.WebSockets(2.1.0-preview2-final)

客户端使用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");
    });
}
Run Code Online (Sandbox Code Playgroud)

在本地运行时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
Run Code Online (Sandbox Code Playgroud)

negotiate方法似乎返回对Websockets的支持:

{"connectionId":"ZRniWKpMLMPIyLhS5RSyAg",
 "availableTransports":[
  {"transport":"WebSockets","transferFormats":["Text","Binary"]},
  {"transport":"ServerSentEvents","transferFormats":["Text"]},
  {"transport":"LongPolling","transferFormats":["Text","Binary"]}]
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?或者WSS尚未获得支持?

Mat*_*nta 9

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

应用程序设置中的WebSocket开关

  • 它们仍然在那里,显然距离这个答案已经过去一年多了,用户界面肯定会改变。设置 &gt; 配置,然后在常规设置选项卡中。这可能是您的应用服务定价层的限制? (2认同)
  • 您好,感谢您的重播,并打算对此进行更新。我做了一些研究,结果发现该功能目前不适用于 Linux 主机,但您可以始终启用 ARR Affinity 来实现类似的行为。我确实同意原件是旧的,目的是试图找出我是否遗漏了一些东西。 (2认同)