来自 WebAPI 的 JsonResult 响应,用于 Elasticsearch NEST 查询

JsonResult Response from WebAPI for Elasticsearch NEST query

我创建了 WEBAPI 方法,使用 NEST 搜索 ES。使用 ES 的搜索按预期工作。我遇到的问题是,当我尝试使用 POSTMAN return Json(response) 时,它会抛出此异常:

{ "Message": "An error has occurred.", "ExceptionMessage": "If you use a custom contract resolver be sure to subclass from ElasticContractResolver", "ExceptionType": "System.Exception", "StackTrace": " at Nest.JsonExtensions.GetConnectionSettings(JsonSerializer serializer)\r\n at Nest.VerbatimDictionaryKeysJsonConverter2.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Web.Http.Results.JsonResult1.Serialize()\r\n at System.Web.Http.Results.JsonResult1.Execute()\r\n at System.Web.Http.Results.JsonResult1.ExecuteAsync(CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }

这是我的WEBAPI方法

[System.Web.Http.Route("SearchElastic")]
[System.Web.Http.HttpPost]

public JsonResult<ISearchResponse<T>> SearchElastic([FromBody] ElasticSearchRequest esRequest)
{
    var searchResponse = EsClient.Search<T>(
        "...NEST query..."
                ));

    return Json(searchResponse);
}

//<T> is a custom C# class.

我正在使用 Elasticsearch/NEST 5.x。

错误消息提示问题所在

{ "Message": "An error has occurred.", "ExceptionMessage": "If you use a custom contract resolver be sure to subclass from ElasticContractResolver", "ExceptionType": "System.Exception", "StackTrace": " at ...

它与行有关

    return Json(searchResponse);

Web API 使用的 Json 序列化程序的配置不知道如何处理 NEST 中某些类型的序列化,因为其中一些类型需要 ElasticContractResolver 才能正确序列化。

有几种方法可以解决这个问题,但最直接的两个是:

  1. 将 Web API 使用的 Json 序列化程序配置为 ElasticContractResolver 作为 JsonSerializerSettings 上的 IContractResolver。这里的缺点是,您要序列化为 json 的所有类型都将通过 NEST 的解析器

  2. 避免 Elasticsearch 中的 deserialization/re-serialization 循环 -> 由 NEST 反序列化 -> 由 Web API 重新序列化,方法是使用低级客户端 return 一个字符串或来自 Elasticsearch 的字节数组。如果您不对 searchResponse 进行任何内省,我会推荐这种方法。如果您确实需要检查 searchResponse,您始终可以将字符串或字节数组反序列化为 SearchResponse<T> 作为单独的步骤。

这是 direct dependency on Json.NET has been removed in NEST 6.x

的原因之一