Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

ASP.NET Core: Why an IOptionsSnapshot Injected Into a Singleton Silently Freezes on Its First Configuration Value Forever

- 03.10.26 - ErcanOPAK

⚙️ The Configuration Reload That Never Reached the Singleton

IOptionsSnapshot exists specifically to give you a fresh configuration value on every scoped lifetime – typically once per request – which makes it the obvious choice when you want config changes to apply without restarting the app. The catch is that IOptionsSnapshot is itself registered as a scoped service, and a singleton can only ever be constructed once, the very first time it’s requested. Injecting IOptionsSnapshot into a singleton’s constructor doesn’t make that singleton reload config on every request – it captures exactly one snapshot, taken at startup, and silently keeps using it for the lifetime of the application.

🔎 The Problem

public class FeatureGate
{
    private readonly FeatureOptions _options;

    // Looks like it will pick up live config changes...
    public FeatureGate(IOptionsSnapshot<FeatureOptions> snapshot)
    {
        _options = snapshot.Value; // captured ONCE, at app startup,
                                    // because FeatureGate is a singleton
    }

    public bool IsEnabled(string feature) => _options.Enabled.Contains(feature);
}

// Program.cs
builder.Services.Configure<FeatureOptions>(builder.Configuration.GetSection("Features"));
builder.Services.AddSingleton<FeatureGate>(); // the mismatch is here

// Editing appsettings.json afterward (even with reloadOnChange: true)
// never changes what FeatureGate.IsEnabled() returns again.

✅ Fix: Match the Options Interface to the Service’s Actual Lifetime

  • Use plain IOptions in a singleton only when the value genuinely never needs to change after startup – it behaves exactly the same way IOptionsSnapshot does there, so there’s no benefit to the snapshot type in that spot.
  • If a singleton genuinely needs to see live configuration changes, inject IOptionsMonitor instead, which is designed to work from any lifetime and exposes a CurrentValue property plus an OnChange callback for exactly this situation.
  • Reserve IOptionsSnapshot for services registered as scoped or transient, where a fresh value per request is both possible and meaningful – injecting it into anything with a longer lifetime quietly defeats its entire purpose.

⚠️ Why This Is Easy to Miss

  • The app starts up correctly, the first request reads the right value, and nothing throws – the only symptom is that a config change made later in production never takes effect, which looks like a deployment or caching problem, not a DI lifetime mismatch.
  • The DI container doesn’t warn about this combination at all, since injecting a scoped service into a singleton is only a hard failure when scope validation is explicitly enabled – otherwise it builds and runs with no indication anything is wrong.

IOptionsSnapshot only refreshes as often as the thing holding it is recreated – and a singleton, by definition, never is.

— Backend Architect

Related posts:

.NET Core: Configure CORS for API Security

.NET Core: Configure CORS for API Security

Asp.Net Core: Fix Razor Pages Page Filter Running Before Auth

Asp.Net Core: Fix Razor Pages Page Filter Running Before Auth

ASP.NET Core: Fix a Minimal API Request That Matches the Wrong Handler Because Two Routes Differ Onl...

ASP.NET Core: Fix a Minimal API Request That Matches the Wrong Handler Because Two Routes Differ Onl...

Views: 13
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

WordPress: Fix a Caching Plugin That Keeps Serving an Outdated robots.txt to Search Engines
The AI Prompt That Catches Missing ConfigureAwait Calls and Sync-Over-Async Deadlocks Hiding in Your C# Codebase

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

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

Social

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