检测到属性

时间:2016-02-17 12:52:19

标签: nest

使用版本2.0.2我找不到为Nest.JsonNetSerializer设置序列化程序设置的位置,以避免自我引用循环检测到异常。

我想这个版本2的文档没有更新。

2 个答案:

答案 0 :(得分:3)

NEST回购中有一个PR,解释了如何在版本2.x.x中处理这种情况。

摘要:

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

希望它有所帮助。

答案 1 :(得分:0)

再次对v.5中的处理方式进行了一些重大更改。

我在测试中找到了这个例子,它对我有用......

        /**=== 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