【发布时间】:2016-02-17 12:52:19
【问题描述】:
使用版本 2.0.2 我只是找不到在哪里设置 Nest.JsonNetSerializer 的序列化程序设置以避免自引用循环检测到异常。
而且我猜该文档没有针对版本 2 进行更新。
【问题讨论】:
-
你可以看看我在 “Self Referencing Loop Detected” exception with JSON.Net 页面上的回答。
标签: nest
使用版本 2.0.2 我只是找不到在哪里设置 Nest.JsonNetSerializer 的序列化程序设置以避免自引用循环检测到异常。
而且我猜该文档没有针对版本 2 进行更新。
【问题讨论】:
标签: nest
在 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;
}
}
希望对你有帮助。
【讨论】:
再次在 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);
}
【讨论】: