[Feature Request] Register services as singletons
See original GitHub issueIs your feature request related to a problem? Please describe.
Hi, I see quite big flaw in the library design, that is - it is registered as scoped service, where in reality this is singleton (talking to window.localStorage
).
Using singletons and nested lifetime scopes is really common in GUIs (at least more common than in web servers), and I think frontend library should support such scenarios.
Describe the solution you’d like Register services as singleton, or maybe expose it so user can compose them.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Dependency injection in ASP.NET Core
Overview of dependency injection; Register groups of services with extension methods ... Singleton objects are the same for every request.
Read more >How to register a service with multiple interfaces in ASP. ...
In this post I describe how to register a concrete class with multiple public interfaces in the Microsoft.Extensions.
Read more >Dependency injection guidelines - .NET
If a type or factory is registered as a singleton, the container disposes the singleton automatically. In the following example, the services ......
Read more >Dependency Injection - Singleton? · Issue #504
ConfigureServices is run and an instance is specified with the service registration). Every subsequent request uses the same instance.
Read more >DI : add support to eager load a singleton service · Issue ...
This adds a mechanism to register services to be activated during startup of a host instead of on first access.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think you’ve misunderstood my meaning. By default in ASP.NET Core, scoped services are scoped to a request and singletons are scoped to the lifetime of an application. In Blazor WebAssembly there is no request to scope scoped service too, hence they behave the same as a singleton. Also generally speaking, I don’t believe client-side web applications do create their own scopes. This sounds like a desktop development practice and this isn’t desktop development.
Anyway, as you may have seen, we’re going to be adding the ability to registered services as scoped based on the example provided by @Fabster1993.
That’s not correct, you may create your own lifetime scopes, and that’s really common in GUI apps, where you register context per tab. Lots of other libraries depends on things being singletons.
I do not use server so I had no idea about this 😦
I think libraries should follow this behavior, they do it for a reason.
Maybe there should be two separate methods (building on top of what you said about
IJSRuntime
), to guide people in right direction? Like this:This way both groups of people would get correct defaults, because as you pointed out these environments are totally different, and setting in configuration is something which is easy to miss, and someone could make his/her app around scoped services where it’d break in long run.