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: Fix a Hosted Service Starting Before Its Dependency

- 25.11.26 - ErcanOPAK

⚙️ Asp.Net Core: Fix a Hosted Service Starting Before Its Dependency

IHostedService instances registered via AddHostedService<T>() have their StartAsync called in registration order, but that only guarantees the method call happens in order — if a service kicks off fire-and-forget async work inside its own StartAsync without awaiting it, the method returns before that work (warming a cache, establishing a connection) is actually done, so the next hosted service starts believing the dependency is ready when it genuinely isn’t.

🔎 The Problem

public class CacheWarmupService : IHostedService
{
    public Task StartAsync(CancellationToken ct)
    {
        _ = WarmCacheAsync(ct);   // fire-and-forget, never awaited
        return Task.CompletedTask; // returns immediately, cache still empty
    }
}
// next registered hosted service assumes the cache is already warm

✅ Fix: Await Real Initialization, or Signal Readiness Explicitly

  • Make a hosted service’s own initialization work genuinely awaited inside StartAsync before it returns, rather than starting background work and returning immediately — the host only guarantees startup order, not that a fire-and-forget task inside a previous service has finished.
  • If one hosted service truly needs another’s state, expose that state through a shared singleton with its own readiness signal that the dependent service awaits explicitly, instead of relying on registration order as an implicit contract.
  • Add a startup-time log line marking when each service’s real initialization work completes, not just when StartAsync returns, so a wrong ordering assumption shows up in logs instead of as an intermittent runtime failure.

🚨 Why This Is Dangerous

  • A hosted service that “usually” works because its dependency happens to finish fast enough in most environments can fail unpredictably under load or on a slower machine — exactly the kind of bug that doesn’t reproduce in a quick local test.

The host guarantees the order your services start in — it never promised any of them would be finished by the time the next one begins.

— Backend Architect

Related posts:

ASP.NET Core Startup Is Slow

ASP.NET Core Startup Is Slow

ASP.NET Core: Why the Response Caching Middleware Does Nothing on Any Endpoint That Sets a Cookie He...

ASP.NET Core: Why the Response Caching Middleware Does Nothing on Any Endpoint That Sets a Cookie He...

ASP.NET Core: Fix a Record Type Used as a Minimal API Route Parameter That Silently Fails to Bind Be...

ASP.NET Core: Fix a Record Type Used as a Minimal API Route Parameter That Silently Fails to Bind Be...

Views: 10
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

C#: Fix a Ref Readonly Parameter Silently Copying on Call
Windows 11: Fix Dynamic Lock Missing a Disconnected Phone

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 562 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