Skip to content
This repository was archived by the owner on Dec 26, 2025. It is now read-only.
This repository was archived by the owner on Dec 26, 2025. It is now read-only.

[Question] Dependency Injection in WebAssemblyHost #254

Description

@minimalisticMe

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionQuestion about this projectTriageIssue needs to be triaged

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions