2

I am trying to send complex objects using EasyNetQ but I keep running into this exception because my object contains a list of child objects that reference back to the parent object:

Self referencing loop detected for property 'Parent' with type 'Domain.ParentItem'.
Path 'Entity.Children[0]'."}

I have tried changing the JSON.NET default settings like so, but it doesn't fix the issue:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

How can I change the default settings of the Json serializer so I can get around this issue without having to create separate DTO objects?

Edit: I have tried the suggestions in the possible duplicate (as I put in my original post) and it did not solve the issue.

gwin003
  • 7,432
  • 5
  • 38
  • 59
  • 1
    possible duplicate of [JSON.NET Error Self referencing loop detected for type](http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) – Steve Mitcham Apr 20 '15 at 12:42
  • How are you serializing your objects? Are you sure you are using the default settings? – dbc Apr 20 '15 at 16:53

1 Answers1

0

First of all, you need to create a class that implements the ISerializer interface.

For example:

public class MyCustomSerializaer : ISerializer
{
    // Add appropriate implementation containing your custom 
    // serialization/deserialization logic here
}

Next, you need to create a method that will register an instance of this class with an instance of IServiceRegister:

public void RegisterServices(IServiceRegister serviceRegister)
{
   serviceRegister.Register(serviceProvider => new MyCustomSerializer());
}

Note: for clarity, I am writing RegisterServices as a separate method; however, you could use an Action<IServiceRegister> instead for brevity if you wish.

Finally, provide RegisterServices as an argument when you call RabbitHutch.CreateBus:

var bus = RabbitHutch.CreateBus(connectionString, RegisterServices);

This will ensure that bus will use your custom serialization class instead of EasyNetQ's default serializer. Be careful to ensure that your producers and consumers are using compatible serializer implementation; otherwise, your consumers will receive messages that they are unable to deserialize.

Note that EasyNetQ allows other components to be replaced as well. For more information, see here.

Donut
  • 110,061
  • 20
  • 134
  • 146