SignalR 2.0.0 beta2 IJsonSerializer Extension

I want to add some custom serialization logic so that the converted json contains camel body properties.

For this reason, I tried to replace the default IJsonSerializer with one found in this link:

https://github.com/SignalR/SignalR/issues/500

However, it seems to be a problem. More specifically, the JsonNetSerializer and IJsonSerializer do not exist in any of the signalR assemblies. Are there any changes that have occurred with the recent version of signalR in this regard?

+4
source share
3 answers

In SignalR 2.0 you cannot replace JsonSerializer, there is more abstraction of IJsonSerializer. It is always JSON.NET.

+8
source

Just to clarify this a bit, with SignalR 2 you cannot replace a serializer with one that is not from JSON.NET. However, the JSON.NET serializer used by SinglR can be created and installed using DependacyResolver.

Here's an example of creating a new JsonSerializer to handle reference loops:

  protected void Application_Start() { var serializerSettings = new JsonSerializerSettings(); serializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; serializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; var serializer = JsonSerializer.Create(serializerSettings); GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer); } 
+9
source

Here's an example of overriding Resalver Dependency SignalR using StructureMap. In this particular example, I go over to the camelCase properties and convert the enumerations as strings.

At startup:

 Microsoft.AspNet.SignalR.GlobalHost.DependencyResolver = new StructureMapSignalRDependencyResolver(); 

Here's the class:

 public class StructureMapSignalRDependencyResolver : Microsoft.AspNet.SignalR.DefaultDependencyResolver { public override object GetService(Type serviceType) { object service; if (!serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass) { // Concrete type resolution service = StructureMap.ObjectFactory.GetInstance(serviceType); } else { // Other type resolution with base fallback service = StructureMap.ObjectFactory.TryGetInstance(serviceType) ?? base.GetService(serviceType); } return service; } public override IEnumerable<object> GetServices(Type serviceType) { var objects = StructureMap.ObjectFactory.GetAllInstances(serviceType).Cast<object>(); return objects.Concat(base.GetServices(serviceType)); } } 

And StructureMap was configured with:

 ObjectFactory.Configure(c => { c.Scan(a => { // scan the assembly that SignalR is referenced by a.AssemblyContainingType<AppHost>(); a.WithDefaultConventions(); }); c.For<Newtonsoft.Json.JsonSerializer>() .Singleton() .Use(new Newtonsoft.Json.JsonSerializer { ContractResolver = new SignalRContractResolver(), Converters = { new Newtonsoft.Json.Converters.StringEnumConverter() } }); }); 

Here is the Contract Resolution:

 public class SignalRContractResolver : Newtonsoft.Json.Serialization.IContractResolver { private readonly Assembly _assembly; private readonly Newtonsoft.Json.Serialization.IContractResolver _camelCaseContractResolver; private readonly Newtonsoft.Json.Serialization.IContractResolver _defaultContractSerializer; public SignalRContractResolver() { _defaultContractSerializer = new Newtonsoft.Json.Serialization.DefaultContractResolver(); _camelCaseContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); _assembly = typeof(Connection).Assembly; } public Newtonsoft.Json.Serialization.JsonContract ResolveContract(Type type) { if (type.Assembly.Equals(_assembly)) { return _defaultContractSerializer.ResolveContract(type); } return _camelCaseContractResolver.ResolveContract(type); } } 
+2
source

Source: https://habr.com/ru/post/1495503/


All Articles