2

Using version 2.0.2 I just cannot find where to set the serializer settings for the Nest.JsonNetSerializer to avoid Self referencing loop detected exception.

And i guess that the documentation is not updated for version 2.

pmertz
  • 33
  • 3
  • You might have a look at my answer on **[“Self Referencing Loop Detected” exception with JSON.Net](https://stackoverflow.com/questions/40472419/self-referencing-loop-detected-exception-with-json-net/51235783#51235783)** page. – Murat Yıldız Jul 08 '18 at 20:40

2 Answers2

4

There is one PR in the NEST repo explaining how you can handle this situation in version 2.x.x.

Summary:

var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool, connectionSettings => new MyJsonNetSerializer(connectionSettings))
    .DefaultIndex(indexName)
    .DisableDirectStreaming()
    .PrettyJson();

public class MyJsonNetSerializer : JsonNetSerializer
{
    public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings)
    {
    }

    protected override void ModifyJsonSerializerSettings(Newtonsoft.Json.JsonSerializerSettings settings)
    {
        settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    }
}

Hope it helps.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
Rob
  • 9,664
  • 3
  • 41
  • 43
0

Once again there are some significant changes to how this is handled in v.5.

I found this example in the tests and it worked for me...

        /**=== Overriding Json.NET settings
    *
    * Overriding the default Json.NET behaviour in NEST is an expert behavior but if you need to get to the nitty gritty, this can be really useful.
    */

    /**
     * The easiest way is to create an instance of `SerializerFactory` that allows you to register a modification callback
     * in the constructor
     */
    public void EasyWay()
    {
        var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
        var connectionSettings = new ConnectionSettings(
            pool,
            new HttpConnection(),
            new SerializerFactory((jsonSettings, nestSettings) => jsonSettings.PreserveReferencesHandling = PreserveReferencesHandling.All));

        var client = new ElasticClient(connectionSettings);
    }

https://github.com/elastic/elasticsearch-net/blob/5.x/src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs#L289

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
gnudle
  • 323
  • 1
  • 3
  • 7