# NEST Client - how to resolve elastic search client exceptions please

**URL:** <https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926>\
**Category:** Elasticsearch\
**Created:** [September 24, 2019, 7:20pm UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926 "2019-09-24T19:20:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishnapss](https://avatars.discourse-cdn.com/v4/letter/k/43a26b/32.png) [@krishnapss](https://discuss.elastic.co/u/krishnapss)\
**Post date:** [September 24, 2019, 7:20pm UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926/1 "2019-09-24T19:20:26Z")

</div>

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](http://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](http://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](http://Elasticsearch.Net), Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)  
> at Elasticsearch.Net.RequestPipeline.CallElasticsearch ([Elasticsearch.Net](http://Elasticsearch.Net), Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)  
> at Elasticsearch.Net.Transport1.Request ([Elasticsearch.Net](http://Elasticsearch.Net), Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)

> **Elasticsearch.Net.ElasticsearchClientException** :  
> at Elasticsearch.Net.Transport1.HandleElasticsearchClientException ([Elasticsearch.Net](http://Elasticsearch.Net), Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)  
> at Elasticsearch.Net.Transport1.FinalizeResponse ([Elasticsearch.Net](http://Elasticsearch.Net), Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d)  
> at Elasticsearch.Net.Transport1.Request ([Elasticsearch.Net](http://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](http://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());
        }
    }

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [September 26, 2019, 7:14am UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926/2 "2019-09-26T07:14:43Z")

</div>

Based on

> [@krishnapss](#):
>
> System.Threading.Tasks.TaskCanceledException

It looks like the request is timing out from the client i.e. the request is taking longer to complete than the client has been configured to wait for, so it cancels the request.

By default, the request timeout is set to 1 minute, but this can be changed on `ConnectionSettings`

```auto
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
    .RequestTimeout(TimeSpan.FromMinutes(5));

var client = new ElasticClient(settings);

```

or on a _per request_ basis

```auto
var searchResponse = client.Search<object>(s => s
	.RequestConfiguration(r => r
		.RequestTimeout(TimeSpan.FromMinutes(5))
	)
);

```

If you're running against Elastic Cloud, you may also want to use the [`CloudConnectionPool`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/connection-pooling.html#cloud-connection-pool), which configures the client for communicating with Elastic Cloud.

---

<div class="post-metadata">

**Author:** ![krishnapss](https://avatars.discourse-cdn.com/v4/letter/k/43a26b/32.png) [@krishnapss](https://discuss.elastic.co/u/krishnapss)\
**Post date:** [September 26, 2019, 7:33pm UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926/3 "2019-09-26T19:33:40Z")

</div>

Hi Russ,  
Thank you very much for your help. I will increase the timeout and use the CloudConnectionPool.  
But is increasing the timeout best solution please? (the user response time)?  
What are the known reasons that can cause this timeout please? e.g. **do you think may be the number of requests**? And also what is the best way to identify and set up alerts for this type of problems.

Thank you,  
Krishna

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [September 27, 2019, 5:02am UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926/4 "2019-09-27T05:02:00Z")

</div>

> [@krishnapss](#):
>
> is increasing the timeout best solution please?

There's many layers to this question to unpack! Some requests may naturally take longer than others, depending on complexity and type of operation. Some of these requests may take longer than the default request timeout of the client of 1 minute so for these you may want to set a longer timeout.

The client sets a default timeout so that it doesn't potentially wait _forever_, keeping a connection open and blocking other outbound connections from being initiated.

If you haven't done so already, it's worth configuring monitoring for your cluster so you can observe the health of the cluster over time.

---

<div class="post-metadata">

**Author:** ![krishnapss](https://avatars.discourse-cdn.com/v4/letter/k/43a26b/32.png) [@krishnapss](https://discuss.elastic.co/u/krishnapss)\
**Post date:** [September 27, 2019, 9:30am UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926/5 "2019-09-27T09:30:17Z")

</div>

Thank you Russ.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 25, 2019, 9:30am UTC](https://discuss.elastic.co/t/nest-client-how-to-resolve-elastic-search-client-exceptions-please/200926/6 "2019-10-25T09:30:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
