+
Skip to content

[Question] Dependency Injection in WebAssemblyHost #254

@minimalisticMe

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载