Search code examples
c#asp.net-core.net-coreasp.net-core-signalr

How to configure JsonOptions for SignalR in .NET Core 2.2?


I want to add the StringEnumConverter to my serializer settings for SignalR but I can't seem to find a way how to do this.

Right now i added the attribute on my transfer object that does the trick but it would be much nicer to define this globally like you can with Mvc

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.Converters.Add(new StringEnumConverter(true));
});

But then for SignalR.


Solution

  • This can be achieved by chaining a call to AddJsonProtocol on to AddSignalR in ConfigureServices. Here's an example:

    services.AddSignalR()
        .AddJsonProtocol(options =>
        {
            options.PayloadSerializerSettings.Converters.Add(new StringEnumConverter(true));
        });
    

    Reference: JSON/MessagePack serialization options