10

I'm using async methods in my WebAPi controllers:

public async Task<HttpResponseMessage> SampleMethod(int subscriptionNumber, DateTime departureDate)
{
    // [...]
}

How do I configure the request timeout? The operation can take up to a couple of minutes and I have to make sure that the request do not timeout.

In MVC there is an attribute called [AsyncTimeout]. Are there an equivalent in WebApi? Can it be configured globally?

1 Answer 1

7

Good question, I would recommend to handle this from client side - you can always specify timeout settings in your consumer code, even if it is ajax:

$.ajax({
url: "/ajax_json_echo/",
timeout: 1000,
...
Sign up to request clarification or add additional context in comments.

3 Comments

Are you saying that IIS/ASP.NET/WebApi2 do not have any built in timeouts?
Yes, they don't have anything so far. WebAPI support cancellation tokens to cancel the operation, so that you can try to use CancellationTokenSource.CancelAfter to achieve that, but as I mentioned - from consuming code
Also the following excerpt reveals a bit more why: "The way to cancel HTTP requests is to cancel them on the HttpClient directly. The reason being that multiple requests can reuse TCP connections within a single HttpClient and so you can't safely cancel a single request without possibly affecting other requests as well."

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.