13

I have a Blazor server side app published on IIS 10. When browsing to an arbitrary page and just letting it idle after a minute or so (sometimes only 45 sec, sometimes something between 1 and two minutes) the modal

Attempting to reconnect to server ...

appears for a couple of seconds. In the browser console the logging shows either

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

or

Information: Connection disconnected.

Since this seems to be a timeout problem I added the following options to ConfigureServices in my startup.cs

services.AddServerSideBlazor()
                .AddHubOptions(options =>
                {
                    options.ClientTimeoutInterval = TimeSpan.FromMinutes(10);
                    options.KeepAliveInterval = TimeSpan.FromSeconds(3);
                    options.HandshakeTimeout = TimeSpan.FromMinutes(10);
                });

This does not solve the problem though.

I also went to the advanced settings of my site in IIS and increased the connection timeout from the default 120 sec to 600 sec. This did not help either.

Those frequent disconnections only happen on the live site hosted on IIS 10. If I start the app locally with Visual Studio the connection is stable.

Any hints of what I'm missing would be appreciated!

Update:

As suggested by @agua from mars in comment below I changed transport type like this

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub(options => { options.Transports = HttpTransportType.LongPolling; });
                endpoints.MapFallbackToPage("/_Host");
            });

With this change the connection is still closed. The console log shows

Information: (LongPolling transport) Poll terminated by server.

I also tried HttpTransportType.ServerSentEvents which does not work at all but gives this error

Error: Failed to start the connection: Error: Unable to connect to the server with any of the available transports. ServerSentEvents failed: Error: 'ServerSentEvents' does not support Binary.

Update 2:

The IIS is configured to use HTTP 1.1 I tried changing to HTTP/2 but this did not change anything regarding the disconnections.

Community
  • 1
  • 1
René
  • 3,413
  • 2
  • 20
  • 34
  • The connection could be closed by a firewall or a proxy. Try to switch to another transport. – agua from mars Feb 04 '20 at 13:12
  • @agua from mars: LongPolling and ServerSentEvents transport types do not work either (see update of question). – René Feb 04 '20 at 14:03
  • Humm, you should open an issue on github repo I guess. The app should try to reconnect after connection loss and long pooling should ever work. – agua from mars Feb 04 '20 at 14:09
  • That said, you probably have an equipment closing the connection. Or your IIS server is configured to use HTTP 1.0 – agua from mars Feb 04 '20 at 14:11
  • @aguafrommars: It does reconnect after connection loss (for both WebSocket and LongPolling). But I get complaints from the users because reconnecting every minute or so makes it look very unstable. I will have another look at the IIS server and the proxy. Thanks for your suggestions! – René Feb 04 '20 at 14:25
  • @aguafrommars: The IIS is configured to use HTTP 1.1. I also tried HTTP/2 (see Update 2). I guess this leaves a proxy as the cause. – René Feb 04 '20 at 14:55
  • try to set hubConnection.serverTimeoutInMilliseconds = 100000; // 100 second. for more information you could refer github link: https://github.com/aspnet/AspNetCore.Docs/issues/6885 – Jalpa Panchal Feb 05 '20 at 09:06
  • @JalpaPanchal: Any hint on how to set hubConnection.serverTimeoutInMilliseconds in a Blazor app? It's not available in the AddServerSideBlazor().AddHubOptions() and not in endpoints.MapBlazorHub() either. – René Feb 05 '20 at 16:34
  • you could refer this link for more detail: [link](https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-3.1&tabs=dotnet#configure-server-options) – Jalpa Panchal Feb 13 '20 at 02:29
  • @René I'm facing the same issue, did you solve it? – Ramy Yousef Apr 29 '20 at 17:52
  • @KasperRoma No I did not "solve" it. But I noticed that lately the problem does not seem to occur anymore. I tried to reproduce the error just now and did not have any success. I don't know what solved it though. I did keep the nuget-packages of my project up to date but I don't know if any of those updates helped. Another thing that's different know from the time of asking this question is that I've been connecting to the web app through a VPN-tunnel for several weeks now (due to corona homeoffice). I don't know it that has any effect. – René May 11 '20 at 09:55

5 Answers5

8

This is related to application pool recycling in IIS as stated by @Programmer. You can reproduce this by going into the application pool, right click the pool and choose recycle to force it. Your blazor app will get the "reconnect modal screen".

For me, I did not want to disable pool recycle, so I added js in the _Hosts.cshtml file as

<script>Blazor.defaultReconnectionHandler._reconnectCallback = function (d) {document.location.reload();}</script>

to automatically reconnect when the server comes back up.

Zahra
  • 2,231
  • 3
  • 21
  • 41
Brian TX
  • 81
  • 1
  • 2
  • 1
    Welcome to Stack Overflow. Normally, we frown upon leaving answers that reiterate existing answers. The exception to that is when you're able to add a lot more explanation to make the guidance more useful to the community—which is _exactly_ what you've done here. Nice job! Keep up the good work. – Jeremy Caney Sep 02 '21 at 01:11
  • I am also suffering from this blazor server reconnecting issue.is there any way to avoid data loss after reconnecting?? cant we store data in browser storage before reconnect issue occurred and cant we fetch that stored data after refreshing or reconnecting the page back?? – buddhi chamalka Dec 12 '22 at 02:48
6

Try this out..

            app.UseEndpoints(endpoints =>
            {
//other settings
.
.
endpoints.MapBlazorHub(options => options.WebSockets.CloseTimeout = new TimeSpan(1, 1, 1));
//other settings
.
.
});
0

This could be related to IIS application pool recycling. Try disabling the recycling to see if that's casing the disconnection.

Programmer
  • 86
  • 3
0

I suffer the same problem on my Blazor server too: Myspector.com

I am sure this comes from network of data provider. I use Othello in Germany with 4G and see disconnection in 5 sec . When I am with wifi with t online on same target server no disconnection at all.

I Think some operators are incompatible with Blazor server/websoscket....

Thierry Brémard
  • 625
  • 1
  • 5
  • 14
0

My recent experience especially on a shared server, increase the pool memory. Connectivity issues went away when we bumped 256MB up to 1GB for a small user base.

bemusical
  • 11
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 09 '21 at 14:52