4

I'm using HttpClient to interact with a webservice (written by my Company) with a lot of apis. All the apis work great except when one of that (the bigger and slower) take more than 100 seconds to give an answer, passed that time i receive the following error(N.B: If the api take less than 100s all works well):

System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: The operation has timed out.
  at System.Net.HttpWebRequest+<RunWithTimeoutWorker>d__241`1[T].MoveNext () [0x000c5] in <3e9b3e26c4694baab3f689687ad40612>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00020] in <3e9b3e26c4694baab3f689687ad40612>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <d4a23bbd2f544c30a48c44dd622ce09f>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () [0x0041d] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0 
   --- End of inner exception stack trace ---
  at System.Net.Http.HttpClientHandler+<SendAsync>d__64.MoveNext () [0x00478] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Net.Http.HttpClient+<SendAsyncWorker>d__49.MoveNext () [0x000ca] in <25ebe1083eaf4329b5adfdd5bbb7aa57>:0 

All the IIS and webservice Timeouts are setted to 5 minutes, so i think i can exclude that is a Webservice Timeout Problem and on the app i set the HttpClient timeout to 10 minutes (but it doesn't work even if i set it at 5 mins), like you can see in my code below. What i'm doing wrong?

CODE:

var handler = new HttpClientHandler() { CookieContainer = cookie,
                                        MaxRequestContentBufferSize = 256000000,
                                        UseCookies = true,
};
HttpClient newclient = new HttpClient(handler);

newclient.Timeout = new TimeSpan(0, 10, 0); //That DIDN'T WORK :(

// ALL THE CODE I NEED TO BUILD MY JSON INSIDE jSoNToPost
//....
//....

//convert it to JSON acceptible content
HttpContent formContent = new StringContent(jSoNToPost, Encoding.UTF8, "application/json");

var uri = new Uri(Path.Combine(BaseUrl, "BIG_API_NAME"));

var response1 = await newclient.PostAsync(uri, formContent); // <-- THE EXCEPTION IS THROWN HERE!!!!!

if (response1.IsSuccessStatusCode)
{
    //Other Stuff
    //....
    //....
}
else
{
    //Other Stuff
    //....
    //....
}

ENVIRONMENT:

Window 10, Visual Studio 2017, Xamarin IOS and Android.

EDIT 2019-01-23 14:21:

The 100s timeout is the default HttpClient timeout, so the

newclient.Timeout = new TimeSpan(0, 10, 0);

not overwrite the default value.


I undestand what is InfiniteTimeSpan and i try to follow both the answers in that post. No success :( it Still show the timeout error after 100s.

10
  • Have you tried setting the Timeout to InfiniteTimeSpan ? Commented Jan 23, 2019 at 11:59
  • iOS? stackoverflow.com/questions/53229040/… Commented Jan 23, 2019 at 12:00
  • What is RunWithTimeoutWorker? Is there a wrapper that maybe timesout? Commented Jan 23, 2019 at 12:01
  • 1
    newclient.Timeout = new TimeSpan(0, 10, 0); What happened when you did this? Did it throw no exception? Throw it after 100 seconds? 5 minutes? 10 minutes? Something else? Commented Jan 23, 2019 at 13:17
  • 1
    @Legion,this issue will be fixed in the next version mono, you can get this info from github.com/mono/mono/issues/12577 Commented Jan 30, 2019 at 7:52

1 Answer 1

1

If you are using load balancers in your environment, make sure to check if there are time limits set at that level. Some cut off sessions after a certain point as a security measure. No code solution to that :(

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.