Skip to content

Commit ce53348

Browse files
authored
[Blazor] Register HttpClient as a scoped instance (#22770)
This avoid the container holding on to transient HttpClient instances for the lifetime of the application.
1 parent 8e813ee commit ce53348

File tree

1 file changed

+2
-2
lines changed
  • src/ProjectTemplates/ComponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client

1 file changed

+2
-2
lines changed

src/ProjectTemplates/ComponentsWebAssembly.ProjectTemplates/content/ComponentsWebAssembly-CSharp/Client/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public static async Task Main(string[] args)
2525
builder.RootComponents.Add<App>("app");
2626

2727
#if (!Hosted || NoAuth)
28-
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
28+
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
2929
#else
3030
builder.Services.AddHttpClient("ComponentsWebAssembly_CSharp.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
3131
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
3232

3333
// Supply HttpClient instances that include access tokens when making requests to the server project
34-
builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ComponentsWebAssembly_CSharp.ServerAPI"));
34+
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("ComponentsWebAssembly_CSharp.ServerAPI"));
3535
#endif
3636
#if(!NoAuth)
3737

0 commit comments

Comments
 (0)