这个问题对我来说非常愚蠢。我没有看到最明显的解决方案。
当不再需要对象时,您可以手动调用 DisposeAsync() 方法。
结果,代码将如下所示。代码与模板略有不同,所以部分逻辑放在单独的类中。
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.JSInterop;
using Store.Client;
using Store.Client.Engine;
using System.Globalization;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddLocalization();
var host = builder.Build();
var js = host.Services.GetRequiredService<IJSRuntime>();
var module = await js.InvokeAsync<IJSObjectReference>("import", "./js/scripts.js");
var culture = await module.InvokeAsync<string>("getCulture");
if (culture is not null)
{
Culture.Current = new CultureInfo(culture);
}
else
{
Culture.Current = Culture.Default;
await module.InvokeVoidAsync("setCulture", Culture.Current.Name);
}
await module.DisposeAsync();
await host.RunAsync();