Why does Blazor page create websocket connection automatically?

I created a Blazor project in asp.net core 3.1 and added API support and now was in process of adding Websockets to it.

I followed this tutorial.


Although the websocket middleware and everyting works, the moment I integrated it into Blazor project I am having issues

Hello from 2nd Request Delegate - No WebSocket
WebSocketServerConnectionManager-> AddSocket: WebSocket added with ID: 18a403b3-cbab-4523-b25d-00cddd4f05d5
Message: {"protocol":"blazorpack","version":1}
Unhandled exception. Receive->Close on: 18a403b3-cbab-4523-b25d-00cddd4f05d5
Managed Connections: 0
Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: . Path '', line 1, position 37.
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)

Apparently there seems to be a websocket connection made to the server (even though I did not do anything) as soon as I load the webpage by blazor itself.

The socket message being ==> {"protocol":"blazorpack","version":1}

Of course the code is not handling this format and hence the error (which can be fixed).

But I am curious about this automatic socket connection made to begin with. Anyone know more about the mystery in-built socket messages from Blazor template?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Using Websockets (through SignalR) is how Blazor passes information between the client part and the server part. It’s fundamental to the entire architecture of the platform. It can fall back to http long-polling but this isn’t recommended. See https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/server?view=aspnetcore-3.1#signalr-configuration .

I doubt if you need a specific separate websocket or signalr implementation on top of that (unless it’s to connect to a different server / application separate from the Blazor app).

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x