【问题标题】:JsonResult Response from WebAPI for Elasticsearch NEST query来自 WebAPI 的 JsonResult 响应用于 Elasticsearch NEST 查询
【发布时间】:2019-01-29 00:43:22
【问题描述】:

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

{ "Message": "发生错误。", "ExceptionMessage": "如果您使用自定义合约解析器,请务必从 ElasticContractResolver 子类化", "ExceptionType": "System.Exception", "StackTrace": " 在 Nest.JsonExtensions.GetConnectionSettings(JsonSerializer 序列化程序)\r\n 在 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 取消令牌)\r\n 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }

这是我的 WEB API 方法

[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。

【问题讨论】:

    标签: elasticsearch asp.net-web-api nest elasticsearch-5


    【解决方案1】:

    错误信息提示问题所在

    { "Message": "发生错误。", "ExceptionMessage": "如果你使用 自定义合同解析器一定要从 ElasticContractResolver", "ExceptionType": "System.Exception", "StackTrace": " 在 ...

    它与线有关

        return Json(searchResponse);
    

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

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

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

    2. 通过使用低级客户端从 Elasticsearch 返回字符串或字节数组,完全避免来自 Elasticsearch 的反序列化/重新序列化循环 -> 由 NEST 反序列化 -> 由 Web API 重新序列化。如果您不对searchResponse 进行任何自省,我会推荐这种方法。如果您确实需要检查searchResponse,您始终可以将字符串或字节数组反序列化为SearchResponse&lt;T&gt;,作为单独的步骤。

    这是direct dependency on Json.NET has been removed in NEST 6.x@的原因之一

    【讨论】:

    • 谢谢拉斯。对于#1)。能否请您指出一个如何实现的示例,以及#2)您的意思是,我应该使用 Elastic.Net 而不是 NEST?
    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多