Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
Should I call IAsyncDisposable.DisposeAsync() in Program.cs using JS module?

I need to configure localization in Blazor WASM, .NET 6

According to the manual, I need to use JavaScript in the Program.cs file. Microsoft recommends connecting JavaScript code through JavaScript isolation in JavaScript modules. However, the code specified in the example is the Blazor component, and IAsyncDisposable.DisposeAsync() is called there

Do I need to call IAsyncDisposable.DisposeAsync() in Program.cs and if so, how to do it without resorting to creating the Program class and the Main method, but using a new version of the console program template?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The question is very stupid on my part. I didn't see the most obvious solution.

You can just call the DisposeAsync() method manually when the object is no longer needed.

As a result, the code will look like this. The code is slightly different from the template, so part of the logic is placed in a separate class.

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();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!