Skip to content

AddHttpMessageHandler not using correct scope in Blazor Wasm app #23847

@mrpmorris

Description

@mrpmorris

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.

To Reproduce

Create a new Blazor WASM project

Add NuGet reference Microsoft.Extensions.Http

Create a class as follows

	public class MyDelegatingHandler : DelegatingHandler
	{
		private readonly IServiceProvider ServiceProvider;

		public MyDelegatingHandler(IServiceProvider serviceProvider)
		{
			ServiceProvider = serviceProvider;
		}

		protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
		{
			Console.WriteLine("Http service provider hash = " + ServiceProvider.GetHashCode());
			return base.SendAsync(request, cancellationToken);
		}
	}

In Program.cs remove the registration of HttpClient and replace it with the following

			builder.Services.AddScoped<MyDelegatingHandler>();
			builder.Services
					.AddHttpClient("local", c => c.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
					.AddHttpMessageHandler<MyDelegatingHandler>();

Edit FetchData.razor
Remove the injection of HttpClient and relace it with

@inject IHttpClientFactory HttpClientFactory
@inject IServiceProvider ServiceProvider

Replace OnInitializedAsync with the following code

protected override async Task OnInitializedAsync()
{
	var httpClient = HttpClientFactory.CreateClient("local");
	Console.WriteLine("Page service provider hash = " + ServiceProvider.GetHashCode());
	forecasts = await httpClient.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
}

Now 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions