Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Asp.Net Core

ASP.NET Core: Fix the Request Timeouts Middleware Canceling a Long-Running Upload Endpoint Because It Inherited the Global Default Policy Instead of Its Own

- 31.10.26 - ErcanOPAK

⚙️ Fix the Request Timeouts Middleware Canceling a Long-Running Upload Endpoint Because It Inherited the Global Default Policy Instead of Its Own

The Request Timeouts middleware, added as a built-in alternative to hand-rolled cancellation-token timeout wrappers, lets you set one default timeout policy for the whole application and then override it per endpoint with a named policy or attribute, which is exactly the flexibility a mix of fast API calls and the occasional genuinely slow operation needs. An endpoint that never gets a policy explicitly attached to it doesn’t opt out of the middleware, it silently inherits whatever the global default policy was configured to be, and a large file-upload endpoint added to the application after that global default was already set up for the rest of the API’s much faster, more typical calls ends up timing out and canceling partway through an upload that was always going to take longer than that default window, with the client simply seeing the connection cut rather than any error that points back to a timeout policy at all.

🔎 The Problem

// Program.cs
builder.Services.AddRequestTimeouts(options =>
{
    // Set once, early on, for the application's typical fast
    // JSON API endpoints:
    options.DefaultPolicy = new RequestTimeoutPolicy
    {
        Timeout = TimeSpan.FromSeconds(15)
    };
});

app.UseRequestTimeouts();

// Added months later, with no policy attached -- it silently
// inherits the 15-second DefaultPolicy above, even though large
// file uploads routinely take well past that:
app.MapPost("/api/reports/upload", async (IFormFile file) =>
{
    await using var stream = file.OpenReadStream();
    await _storage.SaveLargeFileAsync(stream); // can legitimately
                                                // take 60+ seconds
                                                // for a large file
    return Results.Ok();
});

// Client-side symptom: the connection is cut partway through a
// large upload with no readable server error at all, because the
// request was canceled by the timeout middleware, not rejected by
// any validation or storage logic the upload handler itself wrote

✅ Fix: Attach an Explicit Policy to Any Long-Running Endpoint

  • Attach an explicit, longer timeout policy to the upload endpoint specifically, either through a named policy attribute or by disabling the timeout entirely for that one route, rather than assuming an endpoint that handles large files is automatically treated differently from the rest of the API.
  • Audit every endpoint added after the application’s global default policy was first configured, specifically looking for any operation that can legitimately run longer than that default window, since the middleware applies the default silently to anything without its own policy, with no warning at either configuration time or request time.
  • Log the specific cancellation reason when a request is aborted by Request Timeouts middleware, rather than letting it surface only as a generic dropped connection on the client, so a slow endpoint’s real cause shows up immediately in server-side logs instead of needing to be diagnosed by elimination.

🚨 Why This Is Dangerous

  • Because the global default policy was configured correctly for the API it was originally written for, and the upload endpoint’s own code has no bug in it at all, nothing about reading either piece in isolation reveals the problem — the failure only exists in the gap between the two, which is exactly the kind of cross-cutting middleware behavior that’s easy to forget is even active once it’s been set up once early in the project.

A middleware-level default is a decision made once, early, for whatever endpoints existed at the time, and every endpoint added afterward inherits that decision silently unless someone remembers to make a different one for it.

— Backend Architect

Related posts:

ASP.NET Core: Fix SignalR Connections That Keep Dropping Behind a Load Balancer With No Sticky Sessi...

ASP.NET Core: Fix SignalR Connections That Keep Dropping Behind a Load Balancer With No Sticky Sessi...

Options Pattern in ASP.NET Core: Configuration Magic for Robust Apps

Options Pattern in ASP.NET Core: Configuration Magic for Robust Apps

ASP.NET Core: Fix a Request That Keeps Running After the Client Disconnects Because the Action Never...

ASP.NET Core: Fix a Request That Keeps Running After the Client Disconnects Because the Action Never...

Views: 10

Post navigation

C#: Fix a params ReadOnlySpan Overload Still Allocating an Array on Every Call Because Overload Resolution Keeps Picking the Older params Array Version
Photoshop: Fix a Data-Driven Graphics Template’s Variables Silently Stopping to Update Because a Linked Layer Was Renamed After the Data Set Was Already Defined

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com