Kert Kaelep Kert Kaelep - 1 year ago 365
JSON Question

How to set Json Serializer Settings in SignalR2?

I am using Signalr 2, Webapi 2 and Ninject for Ioc and I am in trouble.

In SignalR 2.0 you can't replace the JsonSerializer.
As been answered in here:
SignalR 2.0.0 beta2 IJsonSerializer extensibility

I need to ignore ReferenceLoopHandling in json, so I used this code:

var serializerSettings = new JsonSerializerSettings
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => JsonSerializer.Create(serializerSettings));

It all works fine, but I also call signalR clients from Webapi and calling will not work unless I set this code in app start:

GlobalHost.DependencyResolver = new NinjectSignalRDependencyResolver(NinjectIocConfig.Kernel);

Calling signalR clients from webapi now works but the JSON serialization settings are not used anymore.

How I can get around this or what am I doing wrong?

Thanks in advance

Answer Source

Have you seen my discussion at the end of this issue?

The problem is that kernel.TryGet(serviceType) will not return null but a default instance since singalR is now usign a concrete type and Ninject will not fail on instancing a unregistered concrete type.

You can check if the concrete type has an explicit binding in the Kernel, if not use the value in the SignalR Dep.resolver

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download