[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:
parent
8e813eea24
commit
ce533485f0
|
|
@ -25,13 +25,13 @@ namespace ComponentsWebAssembly_CSharp
|
||||||
builder.RootComponents.Add<App>("app");
|
builder.RootComponents.Add<App>("app");
|
||||||
|
|
||||||
#if (!Hosted || NoAuth)
|
#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
|
#else
|
||||||
builder.Services.AddHttpClient("ComponentsWebAssembly_CSharp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
|
builder.Services.AddHttpClient("ComponentsWebAssembly_CSharp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
|
||||||
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
|
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
|
||||||
|
|
||||||
// Supply HttpClient instances that include access tokens when making requests to the server project
|
// 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
|
#endif
|
||||||
#if(!NoAuth)
|
#if(!NoAuth)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue