Closed
Description
Application developers that need to resolve a resource from their app in Program.MainAsync
have to build the IServiceCollection
and resolve the NavigationManager
from DI. We usually tell users not to build the service container. It would be much more convenient if the interface exposed the URI. In tandem with #19900, this would make doing things at startup much more reasonable. We could even consider removing the AddBaseAddressHttpClient
extension method if this were available:
public sealed class WebAssemblyHostBuilder
{
/// <summary>Gets the <see cref="IWebAssemblyHostEnvironment" />.
+ public IWebAssemblyHostEnvironment HostEnvironment { get; }
}
public interface IWebAssemblyHostEnvironment
{
+ /// <summary> Gets the application base address</summary>
+ public Uri BaseAddress { get; }
}
// Blazor-wasm Template
public static async Task Main(...)
{
- builder.Services.AddBaseAddressHttpClient();
+ builder.Services.AddSingleton(new HttpClient { BaseAddress = builder.HostEnvironment.BaseAddress });
}
The last bit might also play better with users having to configure a custom HttpMessageHandler
on the HttpClient
instance.