SignalR : use camel case

2019-02-07 18:26发布

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.

4条回答
何必那么认真
2楼-- · 2019-02-07 18:48

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>();
查看更多
Emotional °昔
3楼-- · 2019-02-07 18:52

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; }
查看更多
Fickle 薄情
4楼-- · 2019-02-07 18:59

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);
查看更多
Melony?
5楼-- · 2019-02-07 19:01

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/

查看更多
登录 后发表回答