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 a Background Service Swallowing Startup Exceptions

- 17.11.26 - ErcanOPAK

⚙️ Asp.Net Core: Fix a Background Service Swallowing Startup Exceptions

The base BackgroundService class’s own StartAsync kicks off ExecuteAsync and returns immediately without awaiting it to completion, storing the running task internally instead — which means an exception thrown early in ExecuteAsync, even before its first await, lands in a faulted Task that nothing in the default hosting pipeline automatically observes or acts on. The host keeps running exactly as if every background service started successfully, because nothing ever checked whether that stored task actually completed, faulted, or is still running.

🔎 The Problem

public class ReportSyncService : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken token)
    {
        var config = _options.Value.ReportEndpoint
            ?? throw new InvalidOperationException("Missing config");
        // throws synchronously before the first await -
        // the host's StartAsync already returned by the time this
        // fires, and nothing downstream observes the faulted Task
        await DoWorkAsync(config, token);
    }
}

✅ Fix: Observe the Hosted Task and Stop the Host on Fault

  • Explicitly observe the task your background service’s ExecuteAsync produces — wrap it so a fault calls IHostApplicationLifetime.StopApplication(), rather than letting it sit faulted and unobserved while the rest of the app keeps running as if nothing happened.
  • On newer .NET versions, enable BackgroundServiceExceptionBehavior.StopHost (or review your runtime’s current default, since this behavior has changed across .NET versions) so an unhandled exception in a hosted service actually brings the host down instead of disappearing silently.
  • Add a startup-time health check or readiness probe that specifically verifies each critical background service’s task is still running, not just that the web host itself responds — a crashed background service and a healthy web server can coexist indefinitely from the outside.

🚨 Why This Is Dangerous

  • Running for days with a critical background sync service dead on arrival from a startup configuration error, while every health check and dashboard reports green, because none of them check whether the service’s task is actually still alive versus quietly faulted since the moment the process started.

A host that’s still responding to HTTP requests isn’t proof every background service inside it is still alive.

— Backend Architect

Related posts:

.NET Core: Add Health Checks to Monitor Application Status

.NET Core: Add Health Checks to Monitor Application Status

.NET Core: Master Configuration Management

.NET Core: Master Configuration Management

.NET Core: Use Output Caching to Cache Entire HTTP Responses

.NET Core: Use Output Caching to Cache Entire HTTP Responses

Views: 6

Post navigation

Kubernetes: Fix a CronJob Silently Skipping Runs After a Time Change
C#: Fix an IAsyncEnumerable Silently Not Disposing Its Enumerator

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,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

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

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

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