QuickDevGuide

Home › Blazor

Dependency Injection & Services — Blazor

Blazor uses ASP.NET Core's DI container. Inject with @inject in markup or [Inject] in code-behind. Lifetime Blazor Server Blazor WebAssembly Singleton shared by all users on the server one per browser tab (app) Scoped one per circuit (user connection) same as singleton Transient new every injection new every injection Rules On the server, never keep user data in singletons — it leaks between users. DbContext is not thread-safe and circuits are long-lived: inject IDbContextFactory<T> and create a short-lived context per operation. For components that need their own scope, inherit OwningComponentBase<T>. Typed HttpClients (AddHttpClient<T>) for calling APIs — required in WASM.

Key Takeaways