GithubHelp home page GithubHelp logo

Comments (8)

chrissainty avatar chrissainty commented on May 22, 2024

You appear to be mixing constructor injection and property injection in your code sample. If you're injecting the ILocalStorageService into a regular class, just use constructor injection, see code below.

using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

using Blazored.LocalStorage;

using Microsoft.AspNetCore.Components;

namespace Padleloggen.Data
{
    public class DataService
    {
        private HttpClient Http;
        private ILocalStorageService LocalStorage

        public DataService(HttpClient hc, ILocalStorageService ls)
        {
            Http = hc;
            LocalStorage = ls;
        }

        public async Task<string> ConsumeCollection<T>(string sUri) where T : IBaseElement, new()
        {
            List<T> collection = await Http.GetFromJsonAsync<List<T>>(sUri);
            string key = typeof(T).Name.ToString() + "List";
            await LocalStorage.SetItemAsync(key, collection);
            return Http == null ? "null" : key;
        }
    }
}

from localstorage.

rad765 avatar rad765 commented on May 22, 2024

You are right with respect to the mixing of dependency modells, but still I have a problem with LoacalStorageService, It does not inject properly .

The code below is OK, but uncommenting the ILocatStorageService vil give me the following messeage in Blazor: "An unhandled error has occured. Reload;

public class DataService
{
private HttpClient Http { get; set; }

    private ILocalStorageService LocalStorage { get; set; }

    public DataService(HttpClient hc)//, ILocalStorageService ls)
    {
        Http = hc;
  //      LocalStorage = ls;

}

     public async Task<string> ConsumeCollection<T>(string sUri) where T : IBaseElement, new()
     {
         List<T> collection = await Http.GetFromJsonAsync<List<T>>(sUri);
         string key = typeof(T).Name.ToString() + "List";
   //      await LocalStorage.SetItemAsync(key, collection);
         return Http == null ? "null" : key;
     } 

from localstorage.

intech-paul avatar intech-paul commented on May 22, 2024

You could put a try/catch block around the code causing the error to get more info that what you are getting, also you can look the the browser console as well.

try {
Do This();
}
catch (Exception ex)
{
Console.WriteLine(ex.message)
}

The blazor unhandled error is not very helpful.

from localstorage.

chrissainty avatar chrissainty commented on May 22, 2024

@rad765 As this code is in a service, when is it being executed? And are you running this from Blazor Server or Blazor Wasm?

from localstorage.

rad765 avatar rad765 commented on May 22, 2024

I guess, I have learned a lesson about lifetime and scope of services. I was registering my Service as a singleton which probably is OK in Blazor Server. In Blazor Wasm this probably does not make sense. Registering my service as Scoped solved the problem.
Thought your answers/questions did not directly solve my problem, they guided me in the right direction, so thank you very much for the help.

from localstorage.

chrissainty avatar chrissainty commented on May 22, 2024

Glad you got it sorted but I'm a bit concerned about something you said. Singletons in Blazor Server will deliver the same instance to all users of your application, which is usually not the intended behaviour. Singleton and scoped services in Blazor Wasm act exactly the same so it doesn't matter which you choose.

from localstorage.

rad765 avatar rad765 commented on May 22, 2024

And here I thought that I have learned something...
The behaviour is reproduceable. Create at new Blazor wasm project.
Ad this simple service:

        using Blazored.LocalStorage;

        namespace SingletonTest.Data
       {
           public class DataService
          {
                private ILocalStorageService LocalStorage { get; set; }
                public DataService(ILocalStorageService ls)
               {
                    LocalStorage = ls;
               }
         }
     }

In Index.razor add:

        @inject DataService DS

Add this to your Program.cs

       builder.Services
            .AddBlazoredLocalStorage()
            .AddScoped<DataService>();  // This is OK
            //.AddSingleton<DataService>();  // This is not OK

from localstorage.

chrissainty avatar chrissainty commented on May 22, 2024

Ahh yes, it's because the Blazored services are injected as scoped, you can't inject scoped services into singletons as the lifetime of the singleton is longer than the lifetime of the injected service.

This is the standard behaviour of the .NET DI container.

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.