Change default timeout
Solution 1
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:
- 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. - Passing an instance of
CancellationTokenSource
will work if it's timeout is lower thanTimeout
set by theHttpClient
andHttpClient
's timeout is not infinite. Otherwise, theHttpClient
's timeout will take place.
Solution 2
client.Timeout = 5*1000;
doesnt work because client.Timeout
expects something of type: System.TimeSpan
I changed the Timeout value using:
client.Timeout = TimeSpan.FromSeconds(10); // Timeout value is 10 seconds
You can use other methods as well:
Just for FYI:
Default value of Timeout
property is 100 seconds
Solution 3
Since we don't see any task created with a timeout i cannot help.
But if you are using a System.Net.Http under the hood of your application than MSDN says:
The default value is 100,000 milliseconds (100 seconds).
You can than change the value of the HttpClient.Timeout property
clent.Timeout = 5*1000;
casillas
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. (M. Fowler) SOreadytohelp
Updated on August 17, 2021Comments
-
casillas over 2 years
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())) { } } }
-
casillas about 6 yearsWhat would be the side effect if I increase from 100 seconds to 10 minutes? any drawback?
-
Plac3Hold3r about 6 yearsOnly a longer timeout period. You could also set the timeout to
httpClient.Timeout = Timeout.InfiniteTimeSpan;
if you do not want the request to timeout at all. -
Shaiju T about 5 yearsI can set as
client.Timeout = new TimeSpan(0,0,500);
, then what is the use ofCancellationTokenSource
? -
Plac3Hold3r about 5 years@stom The
CancellationTokenSource
would allow you to cancel the request prior to the timeout being triggered. The example would be if a user navigates away and you no longer need the requested data. You can then cancel the request. -
Shaiju T about 5 years@Plac3Hold3r , Good example :) Appreciate.
-
Naveed Ahmed about 3 yearsCan you please elaborate this ` If you want to make it per request you will need to pass through your desired timeout duration as a parameter.` how do we do this?
-
Kool about 2 yearsWhat if I set timeout on HttpClient from HttpClient pool? Is timeout setup reset once the call is completed and HttpClient is returned into the pool?
-
toha almost 2 yearsHi what is the max Timeout Value for this case?
-
SU7 almost 2 years@toha it seems the max value can be set to InfiniteTimeSpan. Please check this doc: docs.microsoft.com/en-us/dotnet/api/…