I am trying to enforce Blazor Interactive Server to use WebSocket only to be able to use it in a load balanced setting. Sticky Session cannot be used due to IT Policy.
When I try running it locally, it works. However, when testing it in one of our Testing environment (which has no load balancer), it returns a 400 error:
wss://<Page>/<Subdirectory>/<BlazorHomePage>/_blazor400 Bad Request
In the Client Browser, it gives this error in the console:
Error: Failed to start the connection: Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.
I can't test it in a load balance setting if it doesn't get past the first Testing environment. Does anyone have a solution for this error?
This is what I have in Program.cs to be able to setup the Transport:
app.UseHttpsRedirection();app.UseStaticFiles();app.UseAntiforgery();app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();app.MapBlazorHub(e =>{ e.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;}).WithOrder(-1);app.Run();
I've added Blazor.start
to configure for WebSocket transport in App.razor:
<head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><base href="@BaseUrl" /><link rel="stylesheet" href="bootstrap/bootstrap.min.css" /><link rel="stylesheet" href="app.css" /><link rel="stylesheet" href="BaseBlazorApp.styles.css" /><link rel="icon" type="image/png" href="favicon.png" /><HeadOutlet @rendermode="InteractiveServer" /></head><body><Routes @rendermode="InteractiveServer" /><script src="_framework/blazor.web.js" autostart="false"></script><script> Blazor.start({ circuit: { configureSignalR: function (builder) { builder.withUrl("_blazor", { skipNegotiation: true, transport: 1 }); } } })</script></body>
Another thing to note is that I have the <base>
set up differently from a standard Blazor project to be able to run the app in a subdirectory without issue, and has been working before.