1

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);
      });
Haha
  • 137
  • 1
  • 1
  • 11
  • Have you checked this doc: https://learn.microsoft.com/en-us/aspnet/core/signalr/troubleshoot?view=aspnetcore-5.0#response-code-404 – Fei Han Mar 26 '21 at 02:06
  • Thanks for your response. Yes I have checked it. ARR affinity is on both for the client and api apps and its using the correct address because its working for longpolling. – Haha Mar 26 '21 at 06:28
  • You can try to enable/allow anonymous access for your hub, and remove `accessTokenFactory` option on client side, then check if it can work well with WebSockets transport on azure app service. – Fei Han Mar 29 '21 at 08:31
  • @FeiHan thanks for reaching out again. Ok, I will check that. – Haha Mar 30 '21 at 07:03
  • @FeiHan it works in web socket, when I remove the accesstokenfactory and set the hub to AllowAnonymous. But I need only authorized people to connect to the hub. what shall I do? – Haha Mar 31 '21 at 06:28
  • Hi @Haha, you can try to modify and increase [the maximum length of the query string or the URL](https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/requestlimits/#attributes), then check if it can help resolve the issue. – Fei Han Mar 31 '21 at 07:08
  • Hi @FeiHan, I have checked the link and I have found out that I have a very big token. About 4355 characters. Im still checking if there is a way to set maxquerystring for azure app service. Thank u very much for your help. – Haha Apr 01 '21 at 18:08
  • Check if your API app service has websockets enabled. It's in the app configuration section. – Prolog Jun 21 '21 at 06:56

1 Answers1

6

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

Websocket configuration

Risky Boy
  • 99
  • 1
  • 5