question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

If you use a custom contract resolver be sure to subclass from ElasticContractResolver

See original GitHub issue

NEST/Elasticsearch.Net version: 5.5.0

Elasticsearch version: 5.6.3

.NET Core version: 2.0.0

Description of the problem including expected versus actual behavior: I’m new to Elasticsearch.NET, and I followed the tutorial. I have something like that:

var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connection = new HttpConnection();
var settings = new ConnectionSettings(connectionPool, connection)
    .DefaultIndex("creators")
    .InferMappingFor<Creator>(i => i
        .TypeName("creator")
        .IndexName("creators")
    );

var elasticClient = new ElasticClient(settings);

var searchRequest = new SearchRequest
{
    From = skip,
    Size = take
};

var response = elasticClient.Search<Creator>(searchRequest);

I just have a simple model:

public class Creator
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string HumanName { get; set; }
}

And I can’t get rid of the Error: System.Exception: If you use a custom contract resolver be sure to subclass from ElasticContractResolver

Steps to reproduce:

  1. Copy/paste the code
  2. Execute it
  3. It should throw the Exception

Provide DebugInformation (if relevant):

System.Exception: If you use a custom contract resolver be sure to subclass from ElasticContractResolver
   at Nest.JsonExtensions.GetConnectionSettings(JsonSerializer serializer) in C:\Users\russ\source\elasticsearch-net-5.x\src\Nest\CommonAbstractions\Extensions\JsonExtensions.cs:line 17
   at Nest.VerbatimDictionaryKeysJsonConverter`2.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) in C:\Users\russ\source\elasticsearch-net-5.x\src\Nest\CommonAbstractions\SerializationBehavior\GenericJsonConverters\VerbatimDictionaryKeysConverter.cs:line 186
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResultFilterAsync>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.<ProcessRequestsAsync>d__2.MoveNext()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DavidRouyercommented, Nov 8, 2017

Thank you for your help!

0reactions
DavidRouyercommented, Nov 20, 2017

Okay, it looks like the ContractResolver can be configured per controller and per method, so I did something like: return Ok(JsonConvert.SerializeObject(results, new JsonSerializerSettings() { ContractResolver = new ElasticContractResolver(connectionSettings, new List<Func<Type, JsonConverter>>()) }));

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonResult Response from WebAPI for Elasticsearch ...
"ExceptionMessage": "If you use a custom contract resolver be sure to subclass from ElasticContractResolver", "ExceptionType": "System.
Read more >
Nest: Add a custom ContractResolver - Elasticsearch
Hi guys,. How can I add a custom ContractResolver in order prevent calling default constructor? Tried to do this, but it does not...
Read more >
Custom Serialization | Elasticsearch .NET Clients [7.17]
Here's another example that implements a custom contract resolver. The custom contract resolver will include the type name within the serialized JSON for...
Read more >
Serialization using ContractResolver
The IContractResolver interface provides a way to customize how the JsonSerializer serializes and deserializes .NET objects to JSON without placing ...
Read more >
Using Custom Contract Resolvers for JSON.NET | Blog
Custom contract resolvers. JSON.Net has a class called DefaultContractResolver that I can inherit and use in my JSONSerializationSettings.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found