How do service lifetimes (Transient, Scoped, Singleton) behave differently in Blazor Server vs. Blazor WebAssembly, and why is 'Scoped' per-circuit in Server but Singleton-like in WASM?
Last updated
Transient means a new instance per request in both models. Singleton means one instance for the app, which on Server is shared by all users, so it must be thread-safe and hold no user state.
Scoped on Server lasts one circuit (the long-lived per-user SignalR connection), not one HTTP request.
In WASM there is one user and no request boundary, so the scope spans the whole app and Scoped acts like Singleton. On Server, keep per-user data in Scoped, never Singleton.
- https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-10.0
- https://blazor-university.com/dependency-injection/dependency-lifetimes-and-scopes/
- https://www.indotalent.com/blog/blazor-server-vs-webassembly-performance-benchmarks.html
- https://learn.microsoft.com/en-us/answers/questions/1279324/what-does-scoped-lifetime-for-a-service-mean-in-bl
- https://www.gapvelocity.ai/blog/blazor-server-vs-webassembly
- https://blazor-university.com/dependency-injection/dependency-lifetimes-and-scopes/comparing-dependency-scopes/
- https://code-maze.com/dependency-injection-lifetimes-aspnet-core/
- https://www.telerik.com/blogs/blazor-basics-dependency-injection-best-practices-use-cases