Skip to content
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

[Question] Dependency Injection in WebAssemblyHost #254

Open
minimalisticMe opened this issue Dec 30, 2024 · 0 comments
Open

[Question] Dependency Injection in WebAssemblyHost #254

minimalisticMe opened this issue Dec 30, 2024 · 0 comments
Labels
Question Question about this project Triage Issue needs to be triaged

Comments

@minimalisticMe
Copy link

minimalisticMe commented Dec 30, 2024

Hi, I am using Blazored.LocalStorage in version 4.5.0 in .NET9.
While I am able to read and write with ILocalStorageService on any razor page, I have troubles in reading values in WebAssemblyHost extension. This also holds true for the sync version ISyncLocalStorageService.

This is my extension

public static async Task<WebAssemblyHost> InitLanguage(this WebAssemblyHost host)
{
    var localStorage = host.Services.GetRequiredService<ILocalStorageService>();
    var result = await localStorage.GetItemAsStringAsync(StorageNames.CurrentCulture);

    if (result == null)
    {
        await localStorage.SetItemAsStringAsync(StorageNames.CurrentCulture, "en-US");
    }

    ...

    return host;
}

StorageNames is a static class holding my strings, so I don't mix up names:

public static class StorageNames
{
    public const string CurrentCulture = "currentCulture";
}

and this is my program.cs (shortened) that calls the extension:

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.InitLog()
    .InitServices();

var host = builder.Build();
await host.InitLanguage();

await host.RunAsync();

I do register LocalStorage with builder.Services.AddBlazoredLocalStorage();.

My issue: In the extension the result value is always null and thus after the check the string "en-US" is written.
And this is the weird part for me: It will write it, but not read it.
When I manually edit the value currentCulture in Chrome Developer Mode and restart the application, the value will be overwritten to "en-US" again.
grafik

What am I doing wrong here?

@minimalisticMe minimalisticMe added Question Question about this project Triage Issue needs to be triaged labels Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Question about this project Triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

1 participant