DefaultHttpClientFactory working incorrectly with Scoped dependencies #42344
Labels
arch-wasm
WebAssembly architecture
area-Extensions-HttpClientFactory
enhancement
Product code improvement that does NOT require public API changes/additions
Milestone
Describe the bug
When using Microsoft.Extensions.Http AddHttpClient and AddHttpMessageHandler, the instance of T belongs to a different ServiceProvider than the ServiceProvider used to generate a Blazor page - even though T is registered as scoped.
This seems to be a problem with DefaultHttpClientFactory being registered as Singleton instead of Scoped. In Blazor every user connection equates to a Scoped container, so when our Http handling delegates have dependencies we really need them to be created in the correct Scoped container.
The Blazor team suggested this should be reported here. Perhaps we could have the option of registering it scoped or something?
dotnet/aspnetcore#23847
To Reproduce
Create a new Blazor WASM project
Add NuGet reference Microsoft.Extensions.Http
Create a class as follows
In
Program.cs
remove the registration of HttpClient and replace it with the followingEdit
FetchData.razor
Remove the injection of HttpClient and relace it with
Replace
OnInitializedAsync
with the following codeNow run the app, open the console window in the browser, and navigate to the Fetch Data page.
Expected
The hash code for the page and the http delegate should be the same.
Actual
They are different, therefore they are both running within separate IServiceProvider containers.
The text was updated successfully, but these errors were encountered: