Change default timeout
Asked Answered
D

3

48

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()))
        {

        }
    }
 }
Dialyser answered 8/1, 2018 at 18:21 Comment(14)
Are you doing a http call inside of your ServiceCall using HttpClient?Aggappe
@Aggappe I have just updated question. Is it good enough?Dialyser
Does ServiceDataAsync make a http call using HttpClient?Aggappe
The reason I ask is that the default timeout of HttpClient is 100 secondsAggappe
@Plac3Hold3r, I have added further.Dialyser
You can always decrease the timeout when creating the taskFreddyfredek
@Dolev, I want to increase to 5 minutes. I need to know how I can change itDialyser
In your AbstractHttpCommand class are you using HttpClient?Aggappe
I have added AbstractHttpCommand.cs as well.Dialyser
We seem to be going deep down the rabbit hole, but I think we are getting there, what does your IHttpService implementation look like, does it have an HttpClient? Do you just want to change the timeout for this one particular request, or all your request?Aggappe
@Plac3Hold3r, I have added IHttpCommand.cs as well. I want to change all.Dialyser
IHttpService implementation ?Aggappe
I have added that one as well.Dialyser
By the way, you shouldn't create HttpClient instance in 'using block' even though it is IDisposable. You should keep one HttpClient object for all your requests and dispose it at the end of application lifetime or when you know that you will not need more requests. Creating new HttpClient for every request will lead you to troubles when you will make requests frequently.Ibsen
A
80

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.
Aggappe answered 8/1, 2018 at 18:56 Comment(7)
What would be the side effect if I increase from 100 seconds to 10 minutes? any drawback?Dialyser
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.Aggappe
I can set as client.Timeout = new TimeSpan(0,0,500); , then what is the use of CancellationTokenSource ?Como
@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.Aggappe
@Aggappe , Good example :) Appreciate.Como
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?Serotonin
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?Mackoff
D
34

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

Duckpin answered 8/7, 2020 at 13:30 Comment(2)
Hi what is the max Timeout Value for this case?Alric
@Alric it seems the max value can be set to InfiniteTimeSpan. Please check this doc: learn.microsoft.com/en-us/dotnet/api/…Duckpin
F
2

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;
Freddyfredek answered 8/1, 2018 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.