Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default timeout

Tags:

I have the following implementation. And the default timeout is 100 seconds.

I wonder how can I able to change the default timeout?

HttpService.cs

public class HttpService : IHttpService {     private static async Task GoRequestAsync<T>(string url, Dictionary<string, object> parameters, HttpMethod method,         Action<T> successAction, Action<Exception> errorAction = null, string body = "")         where T : class     {         using (var httpClient = new HttpClient(new HttpClientHandler()))         {          }     }  } 
like image 303
casillas Avatar asked Jan 08 '18 18:01

casillas


People also ask

How do I change my default timeout?

The Settings screen displays. Select System from the list on the left side of the screen. Select Timeouts. Select the appropriate timeout from the list and then select the Open timeout button.

What is the default timeout?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan.


1 Answers

The default timeout of an HttpClient is 100 seconds.


HttpClient Timeout

You can adjust to your HttpClient and set a custom timeout duration inside of your HttpService.

httpClient.Timeout = 5000;


HttpClient Request Timeout

You could alternatively define a timeout via a cancellation token CancellationTokenSource

using (var cts = new CancellationTokenSource(new TimeSpan(0, 0, 5)) {     await httpClient.GetAsync(url, cts.Token).ConfigureAwait(false); } 

A few notes:

  1. Making changes inside of the HttpClient will affect all requests. If you want to make it per request you will need to pass through your desired timeout duration as a parameter.
  2. Passing an instance of CancellationTokenSource will work if it's timeout is lower than Timeout set by the HttpClient and HttpClient's timeout is not infinite. Otherwise, the HttpClient's timeout will take place.
like image 157
Plac3Hold3r Avatar answered Sep 20 '22 22:09

Plac3Hold3r