Skip to content

Commit

Permalink
feat(templates): Refactor Http client dependencies registration in `B…
Browse files Browse the repository at this point in the history
…oilerplate` #6962 (#6963)
  • Loading branch information
ysmoradi authored Feb 26, 2024
1 parent c66cfee commit 57a2173
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static void AddBlazor(WebApplicationBuilder builder)
apiServerAddress = new Uri(sp.GetRequiredService<IHttpContextAccessor>().HttpContext!.Request.GetBaseUrl(), apiServerAddress);
}

return new HttpClient(sp.GetRequiredKeyedService<HttpMessageHandler>("DefaultMessageHandler"))
return new HttpClient(sp.GetRequiredKeyedService<DelegatingHandler>("DefaultMessageHandler"))
{
BaseAddress = apiServerAddress
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static IServiceCollection AddClientCoreProjectServices(this IServiceColle
services.TryAddTransient<IAuthTokenProvider, ClientSideAuthTokenProvider>();
services.TryAddTransient<IStorageService, BrowserStorageService>();

services.TryAddKeyedTransient<HttpMessageHandler, RequestHeadersDelegationHandler>("DefaultMessageHandler");
services.TryAddKeyedTransient<DelegatingHandler, RequestHeadersDelegationHandler>("DefaultMessageHandler");
services.TryAddTransient<AuthDelegatingHandler>();
services.TryAddTransient<RetryDelegatingHandler>();
services.TryAddTransient<ExceptionDelegatingHandler>();
services.TryAddTransient<HttpClientHandler>();
services.TryAddScoped<HttpClientHandler>();

services.AddScoped<AuthenticationStateProvider, AuthenticationManager>(); // Use 'Add' instead of 'TryAdd' to override the aspnetcore's default AuthenticationStateProvider.
services.TryAddScoped(sp => (AuthenticationManager)sp.GetRequiredService<AuthenticationStateProvider>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void ConfigureServices(this MauiAppBuilder builder)

services.TryAddTransient(sp =>
{
var handler = sp.GetRequiredKeyedService<HttpMessageHandler>("DefaultMessageHandler");
var handler = sp.GetRequiredKeyedService<DelegatingHandler>("DefaultMessageHandler");
HttpClient httpClient = new(handler)
{
BaseAddress = apiServerAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void ConfigureServices(this WebAssemblyHostBuilder builder)
apiServerAddress = new Uri(new Uri(builder.HostEnvironment.BaseAddress), apiServerAddress);
}

services.TryAddTransient(sp => new HttpClient(sp.GetRequiredKeyedService<HttpMessageHandler>("DefaultMessageHandler")) { BaseAddress = apiServerAddress });
services.TryAddTransient(sp => new HttpClient(sp.GetRequiredKeyedService<DelegatingHandler>("DefaultMessageHandler")) { BaseAddress = apiServerAddress });

services.AddClientWebProjectServices();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void ConfigureServices(this IServiceCollection services)
Uri.TryCreate(configuration.GetApiServerAddress(), UriKind.Absolute, out var apiServerAddress);
services.TryAddTransient(sp =>
{
var handler = sp.GetRequiredKeyedService<HttpMessageHandler>("DefaultMessageHandler");
var handler = sp.GetRequiredKeyedService<DelegatingHandler>("DefaultMessageHandler");
HttpClient httpClient = new(handler)
{
BaseAddress = apiServerAddress
Expand Down

0 comments on commit 57a2173

Please sign in to comment.