SignalR Core在Azure App Service中不使用Websockets [英] SignalR Core not using Websockets in Azure App Service

查看:132
本文介绍了SignalR Core在Azure App Service中不使用Websockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用以下堆栈的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?

解决方案

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

I'm working on a SignalR Core web app that uses the following stack:

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

The client is using SignalR NPM package (@aspnet/signalr).

The app is configured like so:

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");
    });
}

When running locally in http://localhost, the client connects using WS protocol. But when deployed in Azure App Service, it falls back to SSE.

The browser log shows:

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

The negotiate method seems to return support to Websockets:

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

Am I missing something? Or is WSS not yet supported?

解决方案

To answer my own question, the failure in the Websocket connection was not a problem with ASP.NET Core or the stack, but due to the fact that Azure App Service needs to have Websockets enabled in the Application Settings:

这篇关于SignalR Core在Azure App Service中不使用Websockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆