Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
Aleksandr Albert Avatar asked Mar 03 '23 07:03

Aleksandr Albert


1 Answers

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();
like image 183
Aleksandr Albert Avatar answered Mar 04 '23 23:03

Aleksandr Albert