3

I have a signalr hub well configured and communicating well with the client. But my hub method accepts parameters of a specific type, and the class representing this type has Pascal cased properties. As shown below:

    public List<DataChanges> Changes { get; set; }

But when the client (Javascript client) calls this method, it passes the object correctly but, the Property of this object is always null unless I change the property's name to "CamelCase". As shown below:

    public List<DataChanges> changes { get; set; }

What I tried:

I added the following attributes each at its own turn to the properties of the object, But the error persisted.

    [DataMember(Name = "changes")]
    [JsonProperty("changes")]

In my startup class, I added signalr json protocol as follows:

 .AddNewtonsoftJsonProtocol(options =>
        {
            //options.PayloadSerializerSettings.ContractResolver = new SignalRContractResolver();
            //options.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver { NamingStrategy = { OverrideSpecifiedNames = true } };
        })

Here is the Signalr contract resolver:

public class SignalRContractResolver : IContractResolver
{
    private readonly Assembly _assembly;
    private readonly IContractResolver _camelCaseContractResolver;
    private readonly IContractResolver _defaultContractSerializer;

    public SignalRContractResolver()
    {
        _defaultContractSerializer = new DefaultContractResolver();
        _camelCaseContractResolver = new CamelCasePropertyNamesContractResolver();
        _assembly = typeof(Startup).GetTypeInfo().Assembly;
    }


    public JsonContract ResolveContract(Type type)
    {
        if (type.GetTypeInfo().Assembly.Equals(_assembly))
            return _defaultContractSerializer.ResolveContract(type);

        return _camelCaseContractResolver.ResolveContract(type);
    }

}

I tried a few other solutions, but nothing worked. The camel case serialization could not be applied by signalr and my property was always null.

Can somebody please tell me a solution for this ?

Damien Doumer
  • 1,991
  • 1
  • 18
  • 33

1 Answers1

0

You have to add this in the startup project: services.AddSignalR().AddJsonProtocol(options => {options.PayloadSerializerOptions.PropertyNamingPolicy = null }

vannak
  • 51
  • 8