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

SignalR not serializing/deserializing custom DataMember name


I am working on a project and trying to integrate SignalR into it. I have a class with a data contract which allows me to use an underscore separated format on my client, and standard Pascal Case on the server, something like this:

[DataContract]
public class Foo {

    [DataMember(Name = "first_name")]
    FirstName { get;set; }

    [DataMember(Name = "last_name")]
    LastName { get;set; }

    [DataMember(Name = "phone")]
    Phone { get;set; }
}

This works fine when passing data through a fetch command to the Razor Page OnGet and OnPost methods, but does not work when using SignalR.

When sending data to server via SignalR, first_name and last_name are null, while phone gets sent correctly.

How can I make SignalR respect the DataMember Name when serializing/deserializing?


Solution

  • I was able to resolve this issue by using the Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson package and adding the following in Startup.cs

    services.AddSignalR()
            .AddNewtonsoftJsonProtocol();