Handling Timeouts and Large File Streaming in ASP.NET

Build resilient endpoints that deliver large files and stay responsive under heavy load

Posted by Hüseyin Sekmenoğlu on March 16, 2020 Backend Development

In real-world scenarios, web applications often need to serve large files such as videos, backups or archives. At the same time, they must remain responsive and handle timeouts gracefully to ensure a smooth user experience. ASP.NET provides several tools and configurations to manage both timeouts and efficient large file streaming.


🧭 Why Timeouts and Streaming Matter

Timeouts are critical to avoid wasting server resources on stalled requests. However, if not configured properly, they can prematurely terminate long-running operations like streaming a large file. Streaming is important because it allows data to be sent in chunks rather than loading the entire file into memory, which is more scalable and memory-efficient.


âš™ Configuring Timeouts in ASP.NET

In ASP.NET Core, timeout behavior is mostly managed through middleware and server configurations.

â–¶ Increase the request timeout in Kestrel

You can configure timeout settings in Program.cs or appsettings.json:

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(5);
    options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
});

These settings allow you to control how long the server will wait for a request to complete before cancelling it.

â–¶ Use CancellationToken in your actions

When working with long-running tasks or file downloads, respect the cancellation token provided by ASP.NET:

public async Task<IActionResult> DownloadFile(CancellationToken cancellationToken)
{
    var filePath = "path/to/largefile.zip";
    var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    return File(fileStream, "application/zip", "largefile.zip");
}

You can monitor cancellationToken.IsCancellationRequested if you need to cancel the operation early.


💾 Streaming Large Files Efficiently

ASP.NET supports streaming files directly from disk to the response without buffering the entire file in memory.

â–¶ Use FileStreamResult for efficient file delivery

public IActionResult StreamLargeFile()
{
    var path = Path.Combine(_env.ContentRootPath, "files", "largevideo.mp4");
    var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
    return new FileStreamResult(fileStream, "video/mp4")
    {
        FileDownloadName = "largevideo.mp4"
    };
}

This approach is highly efficient because it streams the file in chunks as it reads from disk.


🧪 Avoiding Common Pitfalls

  • Do not buffer large files: Use FileStreamResult instead of PhysicalFile with EnableBuffering

  • Test with slow connections: Use tools like Fiddler or tc to simulate slow downloads

  • Configure reverse proxy timeouts: If using NGINX or IIS, make sure their timeout settings are also adjusted


✅ Conclusion

Handling timeouts and streaming large files in ASP.NET requires thoughtful configuration and the right implementation patterns. By streaming files in chunks and managing timeout settings at both the application and server level, you can build APIs and websites that are both fast and reliable under heavy load.