Skip to content

Consider exposing the application url using IWebAssemblyHostEnvironment #19910

Closed
@pranavkm

Description

@pranavkm

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.

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-wasmThis issue is related to and / or impacts Blazor WebAssembly

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions