-
-
Notifications
You must be signed in to change notification settings - Fork 121
[Question] Dependency Injection in WebAssemblyHost #254
Description
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.

What am I doing wrong here?