Can't consume service in webassembly project after 3.2.0-preview3
See original GitHub issueHi Chris, I met a problem after upgrading my project in 3.2.0-preview3 (https://devblogs.microsoft.com/aspnet/blazor-webassembly-3-2-0-preview-3-release-now-available/)
the error i have is
Unhandled exception rendering component: Cannot consume scoped service 'Blazored.LocalStorage.ILocalStorageService' from singleton 'MyService'.
and indeed, i inject your ILocalStorageService in some services inject as singleton.
My project is dual mode serverside/webassembly, and by convention/logic i inject most of my services as singleton in webassembly and scoped in server side.
When i check how you register your service in AddBlazoredLocalStorage, i see you inject it as scoped.
I try to put all my services as scoped in my assembly project (i really don’t want to do that, its just for test) and anyway at the end im unable to resolve all the dependency because the ILogger is still a singleton and i don’t have control on this.
Microsoft don’t really communicate on this update for this preview, but it look like more than a bug resolution than a regression, because for me even with the previous version it was not suppose to work (consume a scoped service from a singleton).
so what do you think about that ?
- a simple resolution will be add a parameter to AddBlazoredLocalStorage to inject services as singleton if asked. I don’t know if it in the good practice.
If you wan’t to reproduce :
- upgrade your webassembly project to the last version (follow this https://devblogs.microsoft.com/aspnet/blazor-webassembly-3-2-0-preview-2-release-now-available/ then this https://devblogs.microsoft.com/aspnet/blazor-webassembly-3-2-0-preview-3-release-now-available/)
- create a simple service where you inject ILocalStorageService
- add this service as a singleton in program.cs
- inject it on your index.razor page
- launch your poject, it will crash with this error in the developer console
I wait for your feedback, if you are OK with the resolution i speak a can suggest a PR (upgrading the package + parameter in AddBlazoredLocalStorage to inject services as singleton)
thank you !
Julien
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Yes that’s why i was surprise also ! but it works ! until the last preview, that’s why i think they resolve a bug
OK it make sense, no problem for me to put all my services in scoped. i double check and if i put all my services scoped it work in the both modes
With passing all my service scoped, i don’t have any problems, i close this issue, thanks for your help !
Hi @julienGrd, I’m really surprised you were ever able to run code where you were injecting scoped services into singletons. The compiler should always have thrown an error as that shouldn’t be allowed to happen.
In terms of your app, why do you choose to use different scoped between Blazor Server and Blazor WebAssembly? There is no difference between scoped and singleton in Blazor WebAssmebly. Hence the guidance from Microsoft to use scoped services as they will act the same between both hosting models.
I really don’t see a reason to change our approach on this. Our services being added to the DI container as scoped is the correct configuration. If you really want to run a non-standard configuration, then instead of using the provided
AddBlazoredLocalStorage
helper for adding services, you can just add them manually with the configuration you choose.