Setting timeout for ASP.NET WebAPI not working

AG 521 Reputation points
2025-05-06T21:09:27.7+00:00

Hi,

I would like to increase timeout for my Web API project to 15 minutes (for testing).
My project uses Kestrel and deployed to IIS on Windows 2019.

I have set IIS Limits -> Connection Time-out (seconds) to 900 and at program.cs I have the following code:

builder.WebHost.ConfigureKestrel(serverOptions =>
{
    serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
    serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(15);
});

app.Use(async (context, next) =>
{
	using var cts = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted);
	cts.CancelAfter(TimeSpan.FromMinutes(15)); // Set timeout here

	try
	{
		context.RequestAborted = cts.Token;
		await next();
	}
	catch (OperationCanceledException)
	{
		context.Response.StatusCode = StatusCodes.Status408RequestTimeout;
		await context.Response.WriteAsync("Request timed out.");
	}
});

When trying to use my client to access my Web API Controller where I placed the following code

await Task.Delay(TimeSpan.FromMinutes(20)).ConfigureAwait(false);

just before "return Ok()"

I am getting 400 Bad Request after about a minute or two also not getting status code 408.

Thanks for any help,

AG

Developer technologies ASP.NET ASP.NET API
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 165 Reputation points Microsoft External Staff
    2025-06-19T10:49:36.5866667+00:00

    Hi AG,

    I have tried researching your problem and recreate it with the provided code. Here are the things that you might need to consider when setting timeout:

    1. Update web.config for ASP.NET Core Module (ANCM)

    If the app is hosted in-process on IIS, ANCM has a default request timeout of 2 minutes. You need to extend it by changing the web.config file:

    <aspNetCore processPath="dotnet"
    			arguments=".\<your_project_name>.dll"
    			stdoutLogEnabled="false"
    			stdoutLogFile=".\logs\stdout"
    			hostingModel="inprocess"
    			requestTimeout="00:15:00" />
    

    You can check out the web.config requestTimeout attribute configuration here: web.config file | Microsoft Learn

    2. Check IIS Application Pool Settings

    Even if the connection timeout is set to 900 seconds, you should consider changing other IIS settings like max request body size, header size, or execution timeout.

    In IIS Manager:

    • Go to Application Pools → Your App Pool → Advanced Settings
    • Set:
    • Ping Maximum Response Time = 900 seconds
      • Shutdown Time Limit = 900 seconds
      • Idle Timeout = 0 (or a high value)

    These might prevent IIS from shutting down the app pool too early.

    3. Middleware problem

    Since your timeout logic already handles Status408RequestTimeout, it's likely that the request is failing before it even reaches that part of your code.

    If possible, could you also share a segment of the IIS logs from your test? That might help pinpoint where the request is getting dropped or why the 400 Bad Request is being returned before your timeout logic kicks in.

    Please let me know if my solutions help in anyway, as I will try to research this problem as you give me more information.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.