-
-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject LocalStorage in C# file #77
Comments
You appear to be mixing constructor injection and property injection in your code sample. If you're injecting the 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;
}
}
} |
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
}
|
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 { The blazor unhandled error is not very helpful. |
@rad765 As this code is in a service, when is it being executed? And are you running this from Blazor Server or Blazor Wasm? |
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. |
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. |
And here I thought that I have learned something...
In Index.razor add:
Add this to your Program.cs
|
Ahh yes, it's because the Blazored services are injected as This is the standard behaviour of the .NET DI container. |
I'm trying to inject LocalStorage in my own Service, sa shown below.
But LocalStorage is still sett to null in the function ConsumeCollection.
The HTTP injection is OK, and injection in a razor file is OK
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
{
[Inject]
private HttpClient Http { get; set; }
}
}
The text was updated successfully, but these errors were encountered: