M-Corp
M-Corp

Reputation: 138

Blazor Server Side - Error: WebSocket closed with status code: 1006 () after 15 seconds

I have a Blazor Server Side app that throws a "Error: WebSocket closed with status code: 1006 () " on the clients side after 15 seconds while waiting for a sql query to complete.

The sql query populates a report so it sometimes takes up to 30 sec to generate. I need to either increase the 15 sec timeout or keep the connection alive long enough for the query to complete.

Does anyone know how I can extend the timeout or keep the page alive long enough for the query to complete ?

Edit: Adding :

endpoints.MapBlazorHub(opts => opts.WebSockets.CloseTimeout = new TimeSpan(1, 1, 1) ); 

to the start.cs seems to increase the time out to 30sec.

Unfortunately this doesn't seem to be enough off a timeout and changing the TimeSpan value any higher does not seem to increase the timeout further. Thanks

Upvotes: 1

Views: 3904

Answers (2)

M-Corp
M-Corp

Reputation: 138

I managed to "fix" this issue by doing a few things.

First I added

endpoints.MapBlazorHub(opts => opts.WebSockets.CloseTimeout = new TimeSpan(1, 1, 1));

to

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

in the

Startup.cs

This didn't completely work, it just increased the timeout from 15 sec to 30sec.

After playing around a bit, I updated the project from .net5 to .net 6 and updated all my nuget packages.

This seemed to help the most as the report now populates faster (under 30sec).

If I try to generate too large a report that takes over 30sec I now end up with a new error:

Error: Connection disconnected with error 'Error: Server timeout elapsed without receiving a message from the server.'.

If i keep refreshing the page the large reports does seem to eventually load.

For now the above fix helps me with my initial report issue.

If anyone has a real solution to this please let me know.

Edit(12/12/2022):

Finally seem to have a fix for this.

In _Host.cshtml

I added the following inside the "body" tag...

<body>

    <script src="_framework/blazor.server.js" autostart="false"></script>
    <script>
      Blazor.start({
        configureSignalR: function (builder) {
          let c = builder.build();
          c.serverTimeoutInMilliseconds = 3000000;
          c.keepAliveIntervalInMilliseconds = 1500000;
          builder.build = () => {
            return c;
          };
        }
      });
        </script>
</body>

This, coupled with the "endpoints" update seems to have solved my issue correctly.

Upvotes: 1

Dany
Dany

Reputation: 61

You can try:

WebSocket ws;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        ws = new WebSocket(new WebAssemblyHttpMessageHandler());
        ws.SetReceiveTimeout(TimeSpan.FromSeconds(30));
        await ws.ConnectAsync(new Uri("ws://localhost:5000/mywebsocket"));
    }
}

Upvotes: 0

Related Questions