Search code examples
signalrblazor-server-side

Blazor server showing 'Invocation canceled due to the underlying connection being closed'


We have a Blazor server side webapp with signalr hosted on IIS. We added EventListener for Blazor.start() with reconnectionHandler and reconnectionOptions. When opening the site as part of previous tabs or turning on after a long sleep, the website shows the "An error has occurred. This application may no longer respond until reloaded." error and the console error shows

Error: The list of component records is not valid.

Uncaught (in promise) Error: Invocation canceled due to the underlying connection being closed.
    at e.connectionClosed (blazor.server.js:1:27242)
    at e.connection.onclose (blazor.server.js:1:17377)
    at e.stopConnection (blazor.server.js:1:61423)
    at e.transport.onclose (blazor.server.js:1:59077)
    at e.close (blazor.server.js:1:47811)
    at e.stop (blazor.server.js:1:47400)
    at e.<anonymous> (blazor.server.js:1:52950)
    at blazor.server.js:1:49394
    at Object.next (blazor.server.js:1:49499)
    at a (blazor.server.js:1:48266)

There is no load balancing and websockets are enabled in IIS. I checked around on Blazor github and some of the solutions suggested solutions was to add a no-cache meta tag in _Host.cshtml like

<meta http-equiv="Cache-Control" content="no-store" />

Even after adding this, we still get the above errors. Are we missing anything on our end? Any help would be appreciated.

The error does not occur when normally using the application. The signalr works fine.


Solution

  • I had this exact same issue before and found this on github detailing the same issue

    As mentioned by Steve, put the following inside the body tag on _Host.chtml instead of putting it as a meta tag.

    @{
        Response.Headers["Cache-Control"] = "no-store";
    }