Skip to main content

Posts

Showing posts with the label SignalR

ASP.NET Core - SignalR–MessagePack

Out of the box SignalR uses JSON to serialize/deserialize the data you send over the wire. This is great from a readability perspective and makes it easy to see the sent messages in your favorite browser dev tools. Unfortunately JSON is rather verbose which impacts performance and bandwidth usage. But did you know that you can also use MessagePack as an alternative? From the documentation : MessagePack is a binary serialization format that is fast and compact. It's useful when performance and bandwidth are a concern because it creates smaller messages compared to JSON . Because it's a binary format, messages are unreadable when looking at network traces and logs unless the bytes are passed through a MessagePack parser. SignalR has built-in support for the MessagePack format, and provides APIs for the client and server to use. Here is how to enable it: First  you need to download the Microsoft.AspNetCore.SignalR.Protocols.MessagePack NuGet package. Next s...

SignalR–Automatic reconnect

With the release of .NET Core 3.0, Microsoft updated SignalR as well. One of the new features is the re-introduction of Automatic Reconnect. Automatic reconnect was part of the original SignalR for ASP.NET, but wasn’t available (until recently) in ASP.NET Core. The JavaScript client for SignalR can be configured to automatically reconnect using the withAutomaticReconnect method on HubConnectionBuilder . It won't automatically reconnect by default. Without any parameters, withAutomaticReconnect() configures the client to wait 0, 2, 10, and 30 seconds respectively before trying each reconnect attempt, stopping after four failed attempts. You can configure the number of reconnect attempts before disconnecting and change the reconnect timing, by passing an array of numbers representing the delay in milliseconds to wait before starting each reconnect attempt: Watch the video here:

ASP.NET Core 2.2 - SignalR Breaking changes

After a seamlessly flawless upgrade to ASP.NET Core, we got into trouble when using SignalR hubs. In our code we were rather lazy and had allowed CORS for everything: In ASP.NET Core 2.2 it is no longer allowed to combine AllowAnyOrigin and AllowCredentials.  When you try to use the two together, you‘ll see the following warning in the output window: warn: Microsoft.AspNetCore.Cors.Infrastructure.CorsService The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time. Configure the policy by listing individual origins if credentials needs to be supported. Problem is that this warning breaks the preflight OPTIONS check of SignalR and the client can no longer connect to the hubs. To fix this, you have to remove the AllowAnyOrigin method and instead specify the origins explicitly(like it should be in the first place):

Breaking changes when updating SignalR for .NET Core

I had to do some changes to one of our applications and noticed that it was still using the beta release of SignalR. So I thought it would be a good idea to quickly do an update to the release version. There were some breaking changes I was able to fix quite easily: From: To: From: To: From: To: From: To:

ASP.NET Core SignalR– Configure logging

As we don’t have an official release yet of the ASP.NET Core SignalR package, it’s a quest to find the right information in the Github repo . Yesterday I lost some time searching on how to configure logging in SignalR. To enable logging you have to pass an extra parameter to your HubConnection specifying a loglevel: Supported log levels are:

Debugging websockets in Chrome

Debugging websockets using Chrome turns out to be quite simple. Here is a step by step guide: Hit F12 to open the Developer Tools in Chrome Go to the Network tab and select the "WS" filter to only show WebSocket connections.     Open a page where you are using websockets. You should see a request appear in the list.      Click on the name of the request, and select the tab "Frames" . Messages triggered by your client are shown in green, while messages from the server are white.

.NET Core SignalR Client error: System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation

To test a .NET Core SignalR application, I created a sample application(using the full .NET framework) where I included the Microsoft.AspNetCore.SignalR.Client NuGet package and added the following code: However when I tried running this application, it failed with the following error message: System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) I checked all my assembly references but they all seemed OK. As a workaround, I was able to avoid the issue by removing the .WithConsoleLogger() line. Anyone who has an idea what can be wrong? Remark: I think it has to do something with the SignalR client which targets .NET Standard 2.0 and my sample application wich targets .NET Framework 4.7. B...