SignalR 2.0.0 beta2 IJsonSerializer extensibility

前端 未结 3 969
攒了一身酷
攒了一身酷 2021-01-13 10:05

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

For that reason i tried to replace the default IJsonSeriali

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 10:59

    Just to clarify this a bit, as of SignalR 2 you can't replace the serializer with one that isn't from from JSON.NET. However, the JSON.NET serializer used by SinglR can be created and set using the DependacyResolver.

    Here's an example where a new JsonSerializer is created 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); 
      }
    

提交回复
热议问题