Change ReferenceLoopHandling in signal R

I am working on a project where we have two applications; The first is a console application that populates the database, and the second is a self-service signalR service that transfers any changes that occur to the contents of the database.

The console application sends the modified model, and the service publishes it to all interested parties. But there is a problem when the model has circular dependencies. I tried to do something like this:

var config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 

but he does not seem to be making any changes; It still throws a detected binding loop exception for the property

Is there any easy way to set ReferenceLoopHandling globally and make it affect any model the converter is acting on?

+4
source share
1 answer

With SignalR 2, you can use DepandyResolver to replace the Json.Net serializer. To solve the problems with the reference contour in my application, I used the following:

  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); } 

If you are using hubProxy on the client, you will need the same settings:

 hubProxy.JsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; hubProxy.JsonSerializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects; 
+9
source

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


All Articles