@@ -42,6 +42,7 @@ Automatically register services in the dependency injection container using attr
4242 - [ ❌ ATCDIR001: As Type Must Be Interface] ( #-ATCDIR001-as-type-must-be-interface )
4343 - [ ❌ ATCDIR002: Class Does Not Implement Interface] ( #-ATCDIR002-class-does-not-implement-interface )
4444 - [ ⚠️ ATCDIR003: Duplicate Registration with Different Lifetime] ( #️-ATCDIR003-duplicate-registration-with-different-lifetime )
45+ - [ ❌ ATCDIR004: Hosted Services Must Use Singleton Lifetime] ( #-ATCDIR004-hosted-services-must-use-singleton-lifetime )
4546- [ 📚 Additional Examples] ( #-additional-examples )
4647
4748---
@@ -605,13 +606,14 @@ builder.Services.AddDependencyRegistrationsFromApi();
605606## ✨ Features
606607
607608- ** Automatic Service Registration** : Decorate classes with ` [Registration] ` attribute for automatic DI registration
609+ - ** Hosted Service Support** : Automatically detects ` BackgroundService ` and ` IHostedService ` implementations and uses ` AddHostedService<T>() `
608610- ** Interface Auto-Detection** : Automatically registers against all implemented interfaces (no ` As ` parameter needed!)
609611- ** Smart Filtering** : System interfaces (IDisposable, etc.) are automatically excluded
610612- ** Multiple Interface Support** : Services implementing multiple interfaces are registered against all of them
611613- ** Flexible Lifetimes** : Support for Singleton, Scoped, and Transient service lifetimes
612614- ** Explicit Override** : Optional ` As ` parameter to override auto-detection when needed
613615- ** Dual Registration** : Register services as both interface and concrete type with ` AsSelf `
614- - ** Compile-time Validation** : Diagnostics for common errors (invalid interface types, missing implementations)
616+ - ** Compile-time Validation** : Diagnostics for common errors (invalid interface types, missing implementations, incorrect hosted service lifetimes )
615617- ** Zero Runtime Overhead** : All code is generated at compile time
616618- ** Native AOT Compatible** : No reflection or runtime code generation - fully trimming-safe and AOT-ready
617619- ** Multi-Project Support** : Each project generates its own registration method
@@ -1102,6 +1104,41 @@ public class UserServiceScoped : IUserService { }
11021104
11031105** Fix:** Ensure consistent lifetimes or use different interfaces.
11041106
1107+ ### ❌ ATCDIR004: Hosted Services Must Use Singleton Lifetime
1108+
1109+ ** Severity:** Error
1110+
1111+ ** Description:** Hosted services (BackgroundService or IHostedService implementations) must use Singleton lifetime.
1112+
1113+ ``` csharp
1114+ // ❌ Error: Hosted services cannot use Scoped or Transient lifetime
1115+ [Registration (Lifetime .Scoped )]
1116+ public class MyBackgroundService : BackgroundService
1117+ {
1118+ protected override Task ExecuteAsync (CancellationToken stoppingToken )
1119+ {
1120+ return Task .CompletedTask ;
1121+ }
1122+ }
1123+ ```
1124+
1125+ ** Fix:** Use Singleton lifetime (or default [ Registration] ) for hosted services:
1126+
1127+ ``` csharp
1128+ // ✅ Correct: Singleton lifetime (explicit)
1129+ [Registration (Lifetime .Singleton )]
1130+ public class MyBackgroundService : BackgroundService { }
1131+
1132+ // ✅ Correct: Default lifetime is Singleton
1133+ [Registration ]
1134+ public class MyBackgroundService : BackgroundService { }
1135+ ```
1136+
1137+ ** Generated Registration:**
1138+ ``` csharp
1139+ services .AddHostedService <MyBackgroundService >();
1140+ ```
1141+
11051142---
11061143
11071144## 📚 Additional Examples
0 commit comments