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 an Output Cache Tag Invalidation That Silently Does Nothing Because the Tag Was Never Attached to the Response

- 16.10.26 - ErcanOPAK

⚙️ The Cache Tag That Was Never Actually Attached

Output Caching’s tag-based eviction lets you clear exactly the cached responses you need via IOutputCacheStore.EvictByTagAsync, instead of flushing the entire cache – but that only works for responses that were actually tagged when they were cached, through policy.Tag(“…”) on the endpoint’s caching policy. Call EvictByTagAsync with a tag that was never applied to anything – a typo, a tag wired up on a different endpoint, or just forgotten entirely – and the call returns successfully having evicted precisely nothing, while the stale response keeps being served from cache indefinitely.

🔎 The Problem

app.MapGet("/api/products", GetProducts)
   .CacheOutput(policy => policy.Expire(TimeSpan.FromMinutes(10)));
   // No .Tag("products") was ever added to this policy.

app.MapPost("/api/products", async (Product p, IOutputCacheStore store) =>
{
    await SaveProduct(p);

    // This call succeeds with no exception - but since nothing
    // was ever tagged "products", it evicts zero cache entries.
    // The GET above keeps serving its old cached response for
    // the full 10 minutes regardless of this call ever running.
    await store.EvictByTagAsync("products", default);
});

✅ Fix: Actually Tag the Policy You Mean to Evict

  • Add .Tag(“products”) to every caching policy you intend to invalidate by that tag, and keep the tag name as a shared constant instead of a magic string repeated in two unrelated places.
  • Write an integration test that caches a response, calls EvictByTagAsync, and then asserts the next request is actually a cache miss (not just that the eviction call didn’t throw) – that’s the only way to catch a tag that silently never matched anything.
  • When adding a new cached endpoint, decide and write down which write operations are supposed to invalidate it, then verify the tag wiring in both places at once rather than adding caching and invalidation in separate, disconnected changes.

🚨 Why This Is Dangerous

  • EvictByTagAsync has no way to report ‘tag not found’ – there is no exception, no return value indicating zero entries matched, and no log line pointing at the mismatch.
  • Users keep seeing stale data for the full cache duration after every single write, which is easy to misdiagnose as a caching duration problem rather than an invalidation wiring problem.

An eviction call that always succeeds and a tag that was never attached look identical from the outside – the only difference is whether anything was actually there to evict.

— Backend Architect

Related posts:

Why BackgroundService Beats Task.Run in APIs

Why BackgroundService Beats Task.Run in APIs

.NET Core: Use IOptions Pattern for Strongly-Typed Configuration

.NET Core: Use IOptions Pattern for Strongly-Typed Configuration

.NET Core JSON Serializer Pitfalls — Why Your Properties Don’t Serialize

.NET Core JSON Serializer Pitfalls — Why Your Properties Don’t Serialize

Views: 6

Post navigation

CSS: Fix a clip-path Shape That Silently Clips Nothing Because the Element Has No Explicit Size
JavaScript: Fix a parseInt() Call That Silently Stops at the First Non-Digit Character Instead of Throwing on a Malformed Number String

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