SignalR Core在Azure App Service中不使用Websockets

时间:2018-04-13 14:52:38

标签: c# asp.net-core websocket azure-web-sites 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");
    });
}

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方法似乎返回对Websockets的支持:

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

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

1 个答案:

答案 0 :(得分:7)

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

WebSocket switch in Application Settings