33

I would like to know if there is a way to configure SignalR so that the client functions in the hub return objects using camel case.

Thanks.

dafriskymonkey
  • 2,189
  • 6
  • 25
  • 48

5 Answers5

61

Roll your own Conttract resolver like

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(Connection).Assembly;
    }

    public JsonContract ResolveContract(Type type)
    {
        if (type.Assembly.Equals(assembly))
        {
            return defaultContractSerializer.ResolveContract(type);

        }

        return camelCaseContractResolver.ResolveContract(type);
    }

}

Register it like

var settings = new JsonSerializerSettings();
settings.ContractResolver = new SignalRContractResolver();
var serializer = JsonSerializer.Create(settings);
GlobalHost.DependencyResolver.Register(typeof (JsonSerializer), () => serializer);

If you use a custom IoC you can run into problems because JsonSerializer is a concrete type and some IoCs like for example Ninject will inject unbound concrete types. In Ninjects case the solution is to register it with Ninject instead of with SignalRs own DependencyResolver

var settings = new JsonSerializerSettings();
settings.ContractResolver = new SignalRContractResolver();
var serializer = JsonSerializer.Create(settings);
kernel.Bind<JsonSerializer>().ToConstant(serializer);

More info on my blog: http://andersmalmgren.com/2014/02/27/why-overriding-jsonserializer-no-longer-work-in-signalr-2-0/

Anders
  • 17,306
  • 10
  • 76
  • 144
  • 1
    Do you know how to do the same configuration in ASP.NET 5 / SignalR 3? GlobalHost is now replaced with new style of dependency injection... – Mikhail Shilkov Jul 25 '15 at 10:56
  • Sorry havent looked at SignalR 3 yet, will have todo that – Anders Jul 26 '15 at 12:08
  • There is no prerelease of SignalR available? – Anders Jul 28 '15 at 08:02
  • 3
    For anyone running into the "You are using a version of the client that isn't compatible with the server. Client version 1.5, server version undefined." error when upgrading SignalR, look into this answer. Was actually the cause of my problem (I used the "old" method of adding serializer settings). – Index Sep 03 '15 at 13:10
  • Where does this registration take place? Is it in Startup.cs? – smatthews1999 Jun 16 '16 at 11:35
  • Were you register your other IoC stuff – Anders Jun 16 '16 at 12:46
  • No success for me, using an Azure Service Bus backplane :( – BAD_SEED Mar 31 '17 at 13:23
  • Hi! Using SignalR on a web api project the first solution with GlobalHost was working like a charm. Now that I try to move the SignalR Server on a Self Hosted console application using the following approach ( https://learn.microsoft.com/en-us/aspnet/signalr/overview/deployment/tutorial-signalr-self-host ) the prior method of json settings doesn't seem to work. Any suggestions? – Xaris Fytrakis Oct 18 '18 at 11:24
5

Anders answer is correct; I just wanted to add that for anyone using AutoFac instead of Ninject you should use this registration in your startup.cs:

var settings = new JsonSerializerSettings();
settings.ContractResolver = new SignalRContractResolver();
var serializer = JsonSerializer.Create(settings);
builder.RegisterInstance(serializer).As<JsonSerializer>();
ejuhjav
  • 2,660
  • 2
  • 21
  • 32
John Reilly
  • 5,791
  • 5
  • 38
  • 63
2

For use with ASP .NET Core, you can register the JsonSerializer like this in Startup -> ConfigureServices:

    var settings = new JsonSerializerSettings { ContractResolver = new SignalRContractResolver() };
    var serializer = JsonSerializer.Create(settings);
    services.AddSingleton(serializer);
Luca Ritossa
  • 1,118
  • 11
  • 22
Francis
  • 1,214
  • 12
  • 19
2

Proper way to configure signalR serialization settings for asp.net core is:

// Inside your Startup.ConfigureServices

var settings = new JsonSerializerSettings
{
    // ... your serialization settings 
};

services.AddOptions<JsonHubProtocolOptions>()
        .Configure(x => x.PayloadSerializerSettings = settings);
lorond
  • 3,856
  • 2
  • 37
  • 52
0

If you don't want to meddling with SignalR config or found it too much hassles, you can add the JsonProperty attribute to specify the property name after serialization on your model. JsonProperty is from JSON.NET, which SignalR uses for serialization.

[JsonProperty("id")]
public byte Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }
ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
Adamy
  • 2,789
  • 3
  • 27
  • 25