Search code examples
c#asp.net-coresignalrasp.net-core-signalr

SignalR hub not found after website is published


I have an ASP.Net Core 3.1 WebApp which includes Razor Pages, Api controllers and a SignalR hub. My Startup.cs looks like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddRazorPages();
    services.AddSignalR(options =>
    {
        options.EnableDetailedErrors = true;
    });
    services.AddMvc();
    services.AddSingleton<HubManager>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseDeveloperExceptionPage();
    app.UseAuthentication();
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapHub<MainHub>("/MainHub");
    });
}

In my razor page I add the following at the end of the script:

<script src="~/js/signalr//dist//browser/signalr.js"></script>
<script src="~/js/HubManager.js"></script>

And the HubManager.js looks like this:

const connection = new signalR.HubConnectionBuilder()
    .withUrl("/MainHub", {
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets
    })
    .configureLogging(signalR.LogLevel.Information)
    .withAutomaticReconnect()
    .build();

async function start() {
    try {
        await connection.start();
        console.log("connected");
    } catch (err) {
        console.log(err);
        setTimeout(() => start(), 5000);
    }
};

connection.onclose(async () => {
    await start();
});

start();

I then publish the webapp and navigate to the page that uses SignalR (using Chrome). But when I check the console, I see that SignalR is polling around every 1s and this error keeps on showing:

signalr.js:4709 WebSocket connection to 'wss://myurl/MainHub' failed: Error during WebSocket handshake: Unexpected response code: 404

HubManager.js:61 Error: There was an error with the transport. at WebSocket.webSocket.onerror (signalr.js:4728)

  • https://myurl/MyWebApp/Home/MyPage
  • https://myurl/MyWebApp/js/signalr/dist/browser/signalr.js
  • wss://myurl/MainHub

The Websockets protocol is installed on the server. So what can the problem be?


Solution

  • WebSocket connection to 'wss://myurl/MainHub' failed: Error during WebSocket handshake: Unexpected response code: 404

    I can reproduce same issue, if I host the ASP.NET Core app as an IIS sub-application "MyWebApp" but not set URL with the sub-app's pathbase on SignalR JavaScript client side.

    enter image description here

    To make it work with IIS sub-application, you can try to modify the code as below then republish it to your IIS server.

    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/MyWebApp/MainHub", {
            skipNegotiation: true,
            transport: signalR.HttpTransportType.WebSockets
        })
        .configureLogging(signalR.LogLevel.Information)
        .withAutomaticReconnect()
        .build();