Share via

Server connection problem on a blazor site

Saeed Pooladzadeh 241 Reputation points
2025-01-04T20:52:03.84+00:00

Hi,

I have made this app in blazor:

Index

Occasionally, I encounter this error: "Attempting to connect to the server." Do you know how to solve it?

regards,

Saeed

Developer technologies | .NET | Blazor
Developer technologies | ASP.NET | Other
{count} votes

3 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 6,455 Reputation points Microsoft External Staff Moderator
    2025-07-21T08:52:10.8133333+00:00

    Hi, in Blazor Server, the error "Attempting to connect to the server..." means the SignalR connection was lost between the client and server.

    Here’s a clear checklist to fix or reduce this issue:


    1. Enable WebSockets (required for SignalR)

    On IIS or Azure App Service:

    • IIS: Install the WebSocket Protocol feature via Server Manager.
    • Azure App Service: In the Azure Portal, go to your App Service → ConfigurationGeneral Settings → Enable Web sockets.

    If behind a reverse proxy (e.g., Nginx or Apache): You must forward WebSocket headers:

    For Nginx:

    location /_blazor {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "Upgrade";
    }
    

    1. Prevent Idle Timeout (especially on Azure)

    Azure App Service will unload your app after idle time by default.

    Fix:

    • In the Azure Portal, go to your App Service → Configuration → turn on Always On.

    1. Handle Reconnect Attempts (in Blazor Server)

    You can make Blazor attempt automatic reconnection. Create a JavaScript snippet to override the default SignalR behavior:

    Add to _Host.cshtml (before </body>):

    <script src="_framework/blazor.server.js" autostart="false"></script>
    <script>
        Blazor.start({
            reconnectionOptions: {
                maxRetries: 10,
                retryInterval: 3000
            }
        });
    </script>
    

    1. Increase KeepAliveInterval and Timeout Settings

    In Program.cs:

    builder.Services.AddServerSideBlazor()
        .AddHubOptions(options =>
        {
            options.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
            options.KeepAliveInterval = TimeSpan.FromSeconds(15);
        });
    

    1. Avoid Blocking Calls

    Don’t use Thread.Sleep or heavy synchronous work in components — it can block SignalR pings.

    Use async/await and background services for long tasks.


    1. Inspect Logs for Disconnects

    Enable detailed logging to see disconnection causes:

    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore.SignalR": "Debug",
        "Microsoft.AspNetCore.Http.Connections": "Debug"
      }
    }
    

    1. Network Stability on Client Side

    Sometimes the issue is the user’s browser or Wi-Fi dropping packets.

    You can handle this more gracefully by showing a custom UI when disconnected (Blazor provides OnConnectionUp, OnConnectionDown events with JavaScript interop).

    Hope this helps.

    1 person found this answer helpful.

  2. Anonymous
    2025-01-06T03:06:44.06+00:00

    Hi @Saeed Pooladzadeh,

    This may happen after a period of inactivity for your application, it will show the message "Attempting to reconnect to server...".

    If you are using blazor 7, 8 or previous version, I suggest you could consider upgrading the version to the 9.

    According to this article, you could find:

    • When the user navigates back to an app with a disconnected circuit, reconnection is attempted immediately rather than waiting for the duration of the next reconnect interval. This improves the user experience when navigating to an app in a browser tab that has gone to sleep.
    • When a reconnection attempt reaches the server but the server has already released the circuit, a page refresh occurs automatically. This prevents the user from having to manually refresh the page if it's likely going to result in a successful reconnection.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Brando

    0 comments No comments

  3. Bruce (SqlWork.com) 83,581 Reputation points Volunteer Moderator
    2025-01-04T21:29:34.14+00:00

    This error is from Blazor server. the client code uses signal/r (a persistent connection) to talk to the server. The error means the signal/r connection was broken and the client code needs to reconnect. This could caused by either be a server recycle or crash, or unreliable internet connection (say mobile). Check your server logs.

    another cause is if the tab hosting the blazor app is not the active tab for a period of time, then the browser will kill the connection.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.