# .NET clients - forward respsonse fast and lean when no error occurs

**URL:** <https://discuss.elastic.co/t/net-clients-forward-respsonse-fast-and-lean-when-no-error-occurs/79534>\
**Category:** Elasticsearch\
**Created:** [March 22, 2017, 6:27am UTC](https://discuss.elastic.co/t/net-clients-forward-respsonse-fast-and-lean-when-no-error-occurs/79534 "2017-03-22T06:27:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knacktus](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@knacktus](https://discuss.elastic.co/u/knacktus)\
**Post date:** [March 22, 2017, 6:27am UTC](https://discuss.elastic.co/t/net-clients-forward-respsonse-fast-and-lean-when-no-error-occurs/79534/1 "2017-03-22T06:27:53Z")

</div>

Hi,

I'd like to achieve the following with as little overhead as possible:

```
1. A web app calls an ASP.NET API.
2. ASP.NET performs auth and adds some filter values according to the user's permissions.
3. ASP.NET sends the request to ES.
4. ASP.NET checks the response wether there was an error.
   a. If no error occurs, the response should be forwarded as fast as possible. 
       No deserialization and re-serialization. 
       The fastest possible way - keep the response gzipped as send from ES.
   b. If an error occurs, the error message should be logged.

```

I'm considering using either no client library - using plain http and JSON. But I like the advantages of connection pooling [Elasticsearch.NET](http://Elasticsearch.NET) and NEST are providing.

I'd further opt for [Elasticsearch.NET](http://Elasticsearch.NET) as I don't need and don't want to work with typed objects. But I'm stuck at how to handle the response for the use case described above.

Cheers,

Jan

---

<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:** [March 24, 2017, 6:34am UTC](https://discuss.elastic.co/t/net-clients-forward-respsonse-fast-and-lean-when-no-error-occurs/79534/2 "2017-03-24T06:34:38Z")

</div>

It's possible to return a `Stream` from a request with [Elasticsearch.Net](http://Elasticsearch.Net), forgoing any deserialization of the response and thus allocation of a byte array, json string, etc.

```
var client = new ElasticLowLevelClient();

// the T in Search<T> is the response type
ElasticsearchResponse<Stream> response = client.Search<Stream>(
  "posts",
  "questions",
  new
  {
	  query = new
	  {
		  match_phrase = new
		  {
			  content = "Questions about C#"
		  }
	  }
  });

```

You can then check `.Success` to determine if the response is a successful one for the client. [Elasticsearch.Net](http://Elasticsearch.Net) (and therefore NEST) has some rules around what a successful response is for a given request, for example, a 404 Not Found response for an index exists request is determined to be successful

```
if (!response.Success)
    throw new SomeException();

// will be a stream
var body = response.Body;

```

**You should be aware however** , not all responses can be determined to be _error-free_ from the HTTP response and its status code alone. For example, a bulk response where in the process of performing some operations in the bulk request have errors, will return a 200 OK response; it's only by reading the response body can it be determined what the errors for those failed operations are.

---

<div class="post-metadata">

**Author:** ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)\
**Post date:** [March 24, 2017, 2:14pm UTC](https://discuss.elastic.co/t/net-clients-forward-respsonse-fast-and-lean-when-no-error-occurs/79534/3 "2017-03-24T14:14:59Z")

</div>

Should add we also ship with `VoidResponse` which you can use for `T` which will close the response stream for you. As @forloop mentioned this is only possible for those API's where you do not need further response introspection.

Not closing/disposing the returned `Stream` will lead to connection leaks.

---

<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:** [April 21, 2017, 2:15pm UTC](https://discuss.elastic.co/t/net-clients-forward-respsonse-fast-and-lean-when-no-error-occurs/79534/4 "2017-04-21T14:15:06Z")

</div>

Closed on April 21, 2017, 2:15pm UTC

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