Change default timeout

45,494

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:

  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.

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;
Share:
45,494
casillas
Author by

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, 2021

Comments

  • casillas
    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
    casillas about 6 years
    What would be the side effect if I increase from 100 seconds to 10 minutes? any drawback?
  • Plac3Hold3r
    Plac3Hold3r about 6 years
    Only 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
    Shaiju T about 5 years
    I can set as client.Timeout = new TimeSpan(0,0,500); , then what is the use of CancellationTokenSource ?
  • Plac3Hold3r
    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
    Shaiju T about 5 years
    @Plac3Hold3r , Good example :) Appreciate.
  • Naveed Ahmed
    Naveed Ahmed about 3 years
    Can 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
    Kool about 2 years
    What 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
    toha almost 2 years
    Hi what is the max Timeout Value for this case?
  • SU7
    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/…