[Blazor] Register HttpClient as a scoped instance (#22770)

This avoid the container holding on to transient HttpClient instances for the lifetime of the application.
This commit is contained in:
Javier Calvarro Nelson 2020-06-11 22:12:43 +02:00 committed by GitHub
parent 8e813eea24
commit ce533485f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -25,13 +25,13 @@ namespace ComponentsWebAssembly_CSharp
builder.RootComponents.Add<App>("app");
#if (!Hosted || NoAuth)
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
#else
builder.Services.AddHttpClient("ComponentsWebAssembly_CSharp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ComponentsWebAssembly_CSharp.ServerAPI"));
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ComponentsWebAssembly_CSharp.ServerAPI"));
#endif
#if(!NoAuth)