Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure request timeout for WebApi controllers

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?

like image 384
jgauffin Avatar asked May 13 '14 12:05

jgauffin


People also ask

What is API request timeout?

Timeouts happen if a service takes more than 30 seconds to respond to a call. If a timeout occurs, you'll see the 500 error status code with details about the timeout in the response. Timeouts are typically caused by one of two things: The call involves too much data. There is a network/service issue.

How do I set HttpWebRequest timeout?

Quick snippet of code: HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest. Create(url); webReq. Timeout = 5000; HttpWebResponse response = (HttpWebResponse)webReq.


1 Answers

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,
...
like image 109
Borys Generalov Avatar answered Oct 21 '22 04:10

Borys Generalov