I can't get the timeout to automatically trigger in the ASP.NET Core 8.0 Preview.
https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRequestTimeouts();
var app = builder.Build();
app.UseRequestTimeouts();
app.MapGet("/", async (HttpContext context) =>
{
try
{
await Task.Delay(TimeSpan.FromSeconds(10), context.RequestAborted);
}
catch (TaskCanceledException)
{
return Results.Content("Timeout!", "text/plain");
}
return Results.Content("No timeout.", "text/plain");
})
.WithRequestTimeout(TimeSpan.FromSeconds(1));
app.Run();
Expected output: Trigger a timeout after 1 second
Actual output: Returns success after 10 seconds
When you run your application with a debugger attached the request timeout check is skipped.
Microsoft.AspNetCore.Http.Timeouts.RequestTimeoutsMiddleware
that is added with UseRequestTimeouts
contains the following code:
internal sealed class RequestTimeoutsMiddleware
{
public Task Invoke(HttpContext context)
{
if (Debugger.IsAttached)
{
return _next(context);
}
...
}
}