Haha
Haha

Reputation: 167

Signalr in azure app service not working for websockets

I have a client (angular) app service and an api(net core 3.1) app service that I am trying to talk to each other through signalr. It works fine on localhost with websocket, but on azure app service it only works for longpolling transport mode even though I have enabled websocket in the configuration. The negotiation returns 200 with the connectionId and all three availableTransports. But the wss request returns 404 as shown below.

WebSocket connection to 'wss://xxxx.azurewebsites.net/notifications?id=ZI0-a_p5G6YziWhCxxc7Kg&access_token=eyJhbGc.. failed: Error during WebSocket handshake: Unexpected response code: 404 
Utils.js:218 [2021-03-25T18:21:13.045Z] Error: Failed to start the transport 'WebSockets': Error: There was an error with the transport.

I have tried to look into a lot of resources and documentations to figure this out, so any help is appreciated. Here are my configurations:

In startup.cs

options.AddPolicy(MyAllowSpecificOrigins,
                    builder =>
                    {
                        builder.WithOrigins("https://xxxxx-dev.azurewebsites.net").AllowAnyMethod().AllowAnyHeader().AllowCredentials();
                        builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader().AllowCredentials();

                    });

....

            services.AddControllers();
            services.AddSignalR();

....
           app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHub<NotificationHub>("/notifications");
            });

In the client side

import { HubConnectionBuilder } from '@microsoft/signalr';

....
    this.hubConnection = new HubConnectionBuilder()
    .withUrl("https://xxxx-dev.net/notifications",
      {
        accessTokenFactory: () => this.authService.getToken(),
        skipNegotiation: false,
        transport: signalR.HttpTransportType.None
     

      }).configureLogging(signalR.LogLevel.Information).build();
    
    this.hubConnection.on("test", (msg) => {
      console.log("pushed data:", msg)
    });

      this.hubConnection.start()
      .then(() => {
        console.log("Connected")
      })
      .catch(err => {
        console.error(err);
      });

Upvotes: 2

Views: 3192

Answers (1)

Risky Boy
Risky Boy

Reputation: 139

As mentioned by @Prolog just turn WebSockets switch to on in your app service configurations. And that should do it.

Websocket configuration

Upvotes: 10

Related Questions