Hello friends, today I came up with a new article in .NET core, Timeout middleware; let’s learn about it and see where we can apply it in real-time.
Real-Time Use Case
In a real-time application, such as a financial trading platform, timely responses are crucial. If a service that provides stock prices or executes trades takes too long to respond, it could lead to significant financial losses. Timeout middleware can be used to ensure that if these services do not respond within a specified time frame, the request is aborted, and an error is returned to the user.
Understanding Timeout Middleware
ASP.NET Core servers don’t do this by default since request processing times vary widely by scenario. For example, WebSockets, static files, and calling expensive APIs would each require a different timeout limit. So ASP.NET Core provides middleware that configures timeouts per endpoint as well as a global timeout.
Benefits of Timeout Middleware
- Improved Reliability: Ensures that your application does not hang indefinitely due to slow external dependencies or internal processing, improving overall reliability.
- Resource Management: Prevents resources from being tied up by long-running requests, freeing them up for other requests.
- User Experience: Provides a better user experience by returning a timely response, even if it’s an error, rather than making users wait indefinitely.
- Error Handling: Allows for a centralized way to handle request timeouts, making it easier to log and manage these occurrences.
- Security: Reduces the attack surface for denial-of-service (DoS) attacks by limiting the time any request can consume server resources.
Learn more about the DoS attack in the below article.
When to Use Timeout Middleware?
Timeout middleware is particularly useful in the following scenarios.
- Preventing Resource Exhaustion: Ensuring that long-running requests do not consume server resources indefinitely.
- Improving User Experience: Providing timely feedback to users when a request cannot be completed within a reasonable timeframe.
- Maintaining Application Responsiveness: Keeping the application responsive by terminating requests that exceed a certain duration.
- Enforcing SLAs (Service Level Agreements): Ensuring that the application meets predefined performance and response time criteria.
- Handling Unpredictable Load: Managing request time during periods of high traffic or when dealing with unpredictable workloads.
Implementation
There are multiple ways to implement it.
- We can create Custom Middleware to configure Timeout
- We can use the [RequestTimeout] attribute at the Controller and Action Level.
1. Custom Timeout Middleware
Consider the code below.
public class TimeoutClass
{
private readonly RequestDelegate _next;
private readonly TimeSpan _timeout;
public TimeoutClass(RequestDelegate next, TimeSpan timeout)
{
_next = next;
_timeout = timeout;
}
public async Task InvokeAsync(HttpContext context)
{
using (var cts = new CancellationTokenSource(_timeout))
{
try
{
context.RequestAborted = cts.Token;
await _next(context);
}
catch (OperationCanceledException)
{
context.Response.StatusCode = StatusCodes.Status504GatewayTimeout;
await context.Response.WriteAsync("Request timed out happening.");
}
}
}
}
We have created a Timeout class and injected RequestDelegate and timeout time via the constructor.
Inside the Invoke Method, we set the Cancellation Token with a specified timeout. When a timeout limit is hit, a CancellationToken in HttpContext.RequestAborted has IsCancellationRequested set to true. Abort() isn't automatically called on the request, so the application may still produce a success or failure response. The default behavior if the app doesn't handle the exception and produce a response is to return status code 504.
Registering TimeoutClass Middleware
public static class TimeoutMiddlewareExtensions
{
public static IApplicationBuilder UseTimeoutMiddleware(this IApplicationBuilder builder, TimeSpan timeout)
{
return builder.UseMiddleware<TimeoutClass>(timeout);
}
}
Program. cs
using Microsoft.AspNetCore.Http.Timeouts;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseTimeoutMiddleware(TimeSpan.FromSeconds(10));
app.UseAuthorization();
app.MapControllers();
app.Run();
Explanation
- Timeoutclass: This middleware class sets a timeout for each request. If the request is not complete within the specified time (_timeout), an OperationCanceledException is thrown, and a 504 Gateway Timeout response is returned.
- UseTimeoutMiddleware: This extension method allows you to add the timeout middleware to the middleware pipeline with a specified timeout.
- Startup Configuration: The middleware is added to the pipeline in the Configure method with a timeout of 10 seconds. Adjust the timeout value based on your application's needs.



Join the conversation! Your thoughts help the community grow.