ConfigurePrimaryHttpMessageHandler<T>() not loading handler in ASP.NET Core 2.2
See original GitHub issueDescribe the bug
Registering an HttpMessageHandler by generic type parameter is causing that handler to not be loaded.
To Reproduce
Steps to reproduce the behavior:
- Using version ‘2.2’ of package ‘Microsoft.AspNetCore.App’, and ‘Microsoft.Extensions.Http’
- Run this code at startup:
services.AddTransient<MyClientHttpMessageHandler>();
services
.AddHttpClient<IMyClient, MyClient>()
.ConfigurePrimaryHttpMessageHandler<MyClientHttpMessageHandler>();
- Make an http client
- Error is that MyClientHttpMessageHandler won’t be loaded or invoked.
Expected behavior
The handler is properly invoked when the client is used.
Workaround
services
.AddHttpClient<IMyClient, MyClient>()
.ConfigurePrimaryHttpMessageHandler(s => s.GetRequiredService<MyClientHttpMessageHandler>())
Additional context
This was the only issue I ran into upgrading a large project from netcoreapp2.1 to netcoreapp2.2.
.NET Core SDK (reflecting any global.json): Version: 2.2.101 Commit: 236713b0b7
Runtime Environment: OS Name: Windows OS Version: 10.0.14393 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.2.101\
Host (useful for support): Version: 2.2.0 Commit: 1249f08fed
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
ConfigurePrimaryHttpMessageH...
Describe the bug Registering an HttpMessageHandler by generic type parameter is causing that handler to not be loaded.
Read more >Make HTTP requests using IHttpClientFactory in ASP.NET ...
Manages the pooling and lifetime of underlying HttpClientMessageHandler instances. Automatic management avoids common DNS (Domain Name System) ...
Read more >aspNetCore 2.2.0 - AspNetCoreModuleV2 error
After updating my project to "Microsoft.AspNetCore.All" 2.2.0, I get an error when running in IIS, but not when running in Visual Studio. HTTP- ......
Read more >Optimally Configuring ASP.NET Core HttpClientFactory
In this post, I'm going to show how to optimally configure a HttpClient using the new HttpClientFactory API in ASP.NET Core 2.1.
Read more >Hosting An ASP.NET Core Web Application In IIS
One issue we have is that for IIS to understand how to talk to .NET Core, it needs a web.config file. Now if...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Pardon my potential ignorance on this subject, but I thought the entire reason why we’ve got HttpClientFactory in .Net Core is help stop people newing up lots of HttpClient instances and using up resources. And I thought the issue really came from having many handlers and not the client itself.
If the work around for this is to use AddTransient then aren’t we creating this same problem and shouldn’t it be AddSingleton?
Until the fix is published to NuGet, the code from https://github.com/aspnet/Extensions/issues/851#issuecomment-456119041 can be used as a workaround.
Example: