GithubHelp home page GithubHelp logo

Comments (7)

julienGrd avatar julienGrd commented on May 23, 2024 2

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 !

from localstorage.

chrissainty avatar chrissainty commented on May 23, 2024 1

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>();

from localstorage.

robertdunaway avatar robertdunaway commented on May 23, 2024

chrissainty I'm having the same problem. So I'm not sure what I should change to make this work properly. You share some code above but you imply this code was for working around and doing some custom implementation.

Do do we inject local storage so we can use it anywhere in our application, correctly?

from localstorage.

chrissainty avatar chrissainty commented on May 23, 2024

Hi @robertdunaway, what exactly is your issue?

from localstorage.

robertdunaway avatar robertdunaway commented on May 23, 2024

I think this might have done the trick for me. I use the BlazoredLocalService as part of my AppStateServce. I made the change below and my AppStateService class was able to continue receiving the injected local state class.

       // builder.Services.AddSingleton<AppStateService>();
        builder.Services.AddScoped<AppStateService>();

from localstorage.

chrissainty avatar chrissainty commented on May 23, 2024

Yes, that's because we register our service as scoped. Scoped services can't be injected into singletons.

from localstorage.

nssidhu avatar nssidhu commented on May 23, 2024

After upgrading to .net 6 preview(blazor wasm) i started getting compiler error.

builder.Services.AddSingleton<ILocalStorageService, LocalStorageService>();
builder.Services.AddSingleton<ISyncLocalStorageService, LocalStorageService>();

image

and if i use the following line i get runtime error

builder.Services.AddBlazoredLocalStorage();

blazor.webassembly.js:1 System.AggregateException: One or more errors occurred. (Cannot consume scoped service 'Blazored.LocalStorage.ILocalStorageService' from singleton 'BlazorBaseApp.Client.Services.CustomAuthenticationStateProvider'.)
 ---> System.InvalidOperationException: Cannot consume scoped service 'Blazored.LocalStorage.ILocalStorageService' from singleton 'BlazorBaseApp.Client.Services.CustomAuthenticationStateProvider'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitScopeCache(ServiceCallSite scopedCallSite, CallSiteValidatorState state) in Microsoft.Extensions.DependencyInjection.dll:token 0x6000096+0x13

from localstorage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.