Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Core not using Websockets in Azure App Service

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?

like image 529
Matias Quaranta Avatar asked Dec 06 '22 12:12

Matias Quaranta


1 Answers

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:

WebSocket switch in Application Settings

like image 139
Matias Quaranta Avatar answered Jan 04 '23 20:01

Matias Quaranta