#856 Ops "read " longer response time throws error in .Net ProjectHaystack.Client

Indumathi Anbumani Thu 17 Sep 2020

Hi,

When using the "Read" ops method from ProjectHaystack.Client nuget package in code below with the filter value that takes >5mins, to receive the output throws error

var auth = new NonHaystackBasicAuthenticator(Username, Password);

var client = new HttpAsyncClient(auth, new Uri(BaseUrl));

client.AuthUri = new Uri(BaseUrl + "user/auth");

await client.OpenAsync();

var response = await client.PostStringAsync("read", filterValue, "application/json", "application/json");

In postman I am able to get successful output using Basic Authentication with myUser and myPass for the same uri "http://myServer/haystack/read" and filter value, but the time taken to get the response is >5mins.

I tested the downloaded project Haystack code by setting the client timeout to 10 mins in class NonHaystackBasicAuthenticator.cs using the code

client.Timeout = TimeSpan.FromMinutes(10);

and I am able to get the expected output successfully in the response of the client.PostStringAsync code.

I had made the change in the class NonHaystackBasicAuthenticator.cs and added the client.timeout before try in Authenticate method, I have attached the complete method below for your reference:

public async Task Authenticate(HttpClient client, Uri authUrl) {

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(_username + ":" + _password)).Trim(=));

client.Timeout = TimeSpan.FromMinutes(10);

try

{

using (var response = await client.GetAsync(authUrl))

{

if ((int)response.StatusCode != 200)

{

throw new AuthException("Basic auth failed: " + response.StatusCode + " " + (await response.Content.ReadAsStringAsync()));

}

}

}

catch (Exception e)

{

throw new AuthException("basic authentication failed", e);

}

}

Could you kindly integrate the code to release a new version of the Nuget package to use in my project directly.

Thanks a lot

Chris Breederveld Thu 17 Sep 2020

Hi Indumathi,

As I would like to not pollute the library with basic communication settings, I would suggest to set the timeout directly on a client that you inject into the HttpAsyncClient class, like so:

var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(10);
var client = new HttpAsyncClient(httpClient, auth, baseUri);

Note that the default HttpClient created when you do not provide it also sets the following, which you may want to see if that is also needed in the above sample:

var handler = new HttpClientHandler() { UseCookies = false, AllowAutoRedirect = false };
var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Add("User-Agent", "HaystackC#");

Indumathi Anbumani Thu 17 Sep 2020

I understand, Thanks for your valuable suggestion Chris.

Login or Signup to reply.