Services
Description
Global service locator providing access to the configured IServiceProvider anywhere in the application — without injection.
Features
Services.Get<T>(): resolve any registered service globally
Behavior
var accessor = Services.Get<IHttpContextAccessor>();
var context = accessor?.HttpContext;
Services.Current is initialized twice:
- At bootstrap with a minimal default provider (HttpContextAccessor, DataProtection, etc.)
- Overwritten when
UseFrameworkBeginMiddlewareis invoked
This ensures framework internals work even before the full DI container is available.
Caveats
- Returns
nullif service is not registered — no exception thrown - Avoid using
Services.Get<T>()in constructors during startup — the full provider may not be initialized yet - This is a service locator — use constructor injection where possible, reserve this for places where injection is not available (static classes, extension methods, filters)
Tip
Use Services.Get<T>() in static contexts where DI injection is not possible — global helpers, static extensions, or framework internals. For regular classes prefer constructor injection.