What does scoped lifetime for a service mean in Blazor (server)?

David Thielen 2,256 Reputation points
2023-05-07T16:24:50.2+00:00

This is for Blazor (server).

According to this documentation, session scope for a service in Blazor (server side) means:

Scoped services aren't reconstructed when navigating among components on the client, where the communication to the server takes place over the SignalR connection of the user's circuit, not via HTTP requests.

And in this blog article (seems to be knowledgable):

It creates a scope for each so-called “circuit”. A circuit is created when a new browser tab connects to the Blazor server via the underlying SignalR transport, and it is terminated when the browser tab is closed again (or if the connection is lost and fails to reconnect within a specified time frame).

But in [this Microsoft Q&A](https://learn.microsoft.com/en-us/answers/questions/1276643/should-this-be-transient-scoped-(and-is-it-a-good) it says:

If this is a Blazor Server question then scoped behaves like a singleton. The official documentation covers how service life time works..

I think a singleton is the same object across all circuits/sessions. The documentation is as follows.

DI creates a single instance of the service. All components requiring a Singleton service receive the same instance of the service.

Of course "all components" can mean all components in a single circuit/session. Or it can mean all components across all circuits/sessions. I can see how if the "all components" is per circuit/session, then scoped is the same as singleton for most (all) use cases. But if singleton is a single instance across ever circuit/session, then they differ - a lot.

So what exactly is going on for scoped & singleton services on Blazor server?

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,386 questions
{count} votes

Accepted answer
  1. VasimTamboli 4,410 Reputation points
    2023-05-07T19:26:18.5966667+00:00

    In Blazor Server, the lifetime of a scoped service is scoped to a single circuit. A circuit is created when a user connects to the server and is maintained for the duration of that user's session. When the user navigates between components within the same circuit, the same instance of the scoped service is used.

    On the other hand, a singleton service is a single instance of the service that is shared across all circuits in the application. All components that require a singleton service receive the same instance of the service.

    So, the statement from the Microsoft Q&A that "scoped behaves like a singleton" is not accurate. While it is true that a scoped service behaves like a singleton within a single circuit, it is not shared across all circuits in the application like a singleton service is.


2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2023-05-10T17:35:40.6633333+00:00

    The answer is incorrect. Blazor singletons are not shared with all Blazer circuits. each Blazor circuit has its own singleton collection.

    currently there is no functional difference between Singleton and Scoped in Blazor.

    1 person found this answer helpful.

  2. Sergey Ivanov 1 Reputation point
    2024-01-29T23:45:35.58+00:00

    What does Scoped mean in the case of WASM (WebAssembly)? I just created a new project from the FluentUI's template https://github.com/microsoft/fluentui-blazor and it added this line in my Program.cs

    builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });