HI,
Could someone please help me to resolve this issue.
We are recently getting Two types of exceptions
Elasticsearch.Net.UnexpectedElasticsearchClientException
The operation was canceled.
Elasticsearch.Net.ElasticsearchClientException
Maximum timeout reached while retrying request. Call: Status code unknown from: POST /indexname/_search?typed_keys=true
We are using Elastic cloud service and we have created asp.net core 2.2 API to communicate with Elasticsearch. For communication we are using NEST Client 7.0. Our service is working ok for some time and started throwing exceptions until we restart the our api server. But if we try to connect to Elastic cloud service from post man directly it is working ok.
Exception details
Elasticsearch.Net.UnexpectedElasticsearchClientException:
at Elasticsearch.Net.Transport1.Request (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
Inner exception System.OperationCanceledException handled at Elasticsearch.Net.Transport1.Request:
at System.Net.Http.HttpClient.HandleFinishSendAsyncError (System.Net.Http, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Net.Http.HttpClient+d__63.MoveNext (System.Net.Http, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Elasticsearch.Net.HttpConnection.Request (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
at Elasticsearch.Net.RequestPipeline.CallElasticsearch (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
at Elasticsearch.Net.Transport1.Request (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
Elasticsearch.Net.ElasticsearchClientException:
at Elasticsearch.Net.Transport1.HandleElasticsearchClientException (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
at Elasticsearch.Net.Transport1.FinalizeResponse (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
at Elasticsearch.Net.Transport1.Request (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)Inner exception System.Threading.Tasks.TaskCanceledException handled at Elasticsearch.Net.Transport1.HandleElasticsearchClientException:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Threading.Tasks.ValueTask1.get_Result (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Net.Http.HttpConnectionPool+d__39.MoveNext (System.Net.Http, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Net.Http.RedirectHandler+d__4.MoveNext (System.Net.Http, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Net.Http.HttpClient+d__63.MoveNext (System.Net.Http, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
at Elasticsearch.Net.HttpConnection.Request (Elasticsearch.Net, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)
I am using singleton ElasticClient in my code. I have created a factory class which is a registered as singleton.
Elastic client creation code
internal class ElasticClientFactory
{
private readonly IOptions<Configuration> _configuration;
private ElasticClient _client;
public ElasticClientFactory(IOptions<Configuration> elasticSearchConfiguration)
{
_client = CreateClient();
}
private ElasticClient CreateClient()
{
var settings = new ConnectionSettings(new Uri(_configuration.Value.Url))
.DefaultMappingFor<Model>(m => m.IdProperty(p => p.Id))
.DefaultFieldNameInferrer(f => f.ToLower())
.ThrowExceptions();
settings.BasicAuthentication(_configuration.Value.Username, _configuration.Value.Password);
return new ElasticClient(settings);
}
public IElasticClient Create()
{
return _client ?? (_client = CreateClient());
}
}