question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't consume service in webassembly project after 3.2.0-preview3

See original GitHub issue

Hi 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 :

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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
julienGrdcommented, Mar 27, 2020

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.

Yes that’s why i was surprise also ! but it works ! until the last preview, that’s why i think they resolve a bug

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.

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

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.

With passing all my service scoped, i don’t have any problems, i close this issue, thanks for your help !

1reaction
chrissaintycommented, Mar 27, 2020

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.

    Services.AddSingleton<ILocalStorageService, LocalStorageService>()
    Services.AddSingleton<ISyncLocalStorageService, LocalStorageService>();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor WebAssembly 3.2.0 Preview 3 release now available
A new preview update of Blazor WebAssembly is now available! Here's what's new in this release: Debugging in Visual Studio and Visual Studio ......
Read more >
Cannot debug client side blazor
Just for curiosity, I created a brand new project. Visual Studio -> Blazor App -> Blazor WebAssembly App Configure for HTTPS is unticked...
Read more >
Updating JSON Validate to Blazor 3.2.0 Preview 3
I built the website JSON Validate with Blazor some time ago for the purposes of trying out Blazor, and have been updating it...
Read more >
Telerik UI for Blazor in a PWA—Easy as Pie
To build this app, we needed to use the “true” Blazor – the Blazor WebAssembly (or Blazor WASM) flavor (Blazor UI components by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found