question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConfigurePrimaryHttpMessageHandler<T>() not loading handler in ASP.NET Core 2.2

See original GitHub issue

Describe the bug

Registering an HttpMessageHandler by generic type parameter is causing that handler to not be loaded.

To Reproduce

Steps to reproduce the behavior:

  1. Using version ‘2.2’ of package ‘Microsoft.AspNetCore.App’, and ‘Microsoft.Extensions.Http’
  2. Run this code at startup:
services.AddTransient<MyClientHttpMessageHandler>();
services
    .AddHttpClient<IMyClient, MyClient>()
    .ConfigurePrimaryHttpMessageHandler<MyClientHttpMessageHandler>();
  1. Make an http client
  2. 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:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
matt-lethargiccommented, Oct 2, 2019

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?

1reaction
sliekenscommented, Jun 14, 2019

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:

services.AddTransient<MyClientHttpMessageHandler>();
services
    .AddHttpClient<IMyClient, MyClient>()
    .ConfigurePrimaryHttpMessageHandler(sp => sp.GetRequiredService<MyClientHttpMessageHandler>());
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found