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.

Adding a typed HTTP client twice, throws exception

See original GitHub issue

EDIT by @rynowak - see below for shiproom template.

Calling

services.AddHttpClient<SomeType>()

more than once throws an exception in 3.0 preview 7 - this used to work in 2.x.

Is that the intended behavior?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
rynowakcommented, Nov 24, 2019

Description

In 3.0 we added validation to HttpClient Factory’s registration code path to prevent some common mistakes. This new validation code blocks some valid use cases that we didn’t know about.

Customer Impact

Attempting to call AddHttpClient<T> twice with the same T throws an exception. This pattern is used when both library code and user code want to collaborate on configuration of a client type.

Regression?

Yes, this is a regression from 2.2

Risk

Low. This add special casing to our validation code path to allow more patterns. The impact of this is that cases that are explicitly blocked by an exception in 3.0 (but were allowed in 2.2) will be allowed again.

2reactions
photomoosecommented, Jan 9, 2020

We’ve just stumbled into this issue after upgrading our projects from 2.2 to 3.1. In 2.2, we were relying on the ability to call AddHttpClient<T>() subsequent times, e.g. in .ConfigureTestServices() to override an HttpClient’s message handler in tests.

           var hostBuilder = new HostBuilder()
                .ConfigureWebHost(builder =>
                {
                    builder.UseTestServer()
                           .UseStartup<Startup>()
                           .ConfigureTestServices(services => 
                           {
                                 services.AddHttpClient<IIdentityServiceClient, IdentityServiceClient>()
                 .ConfigurePrimaryHttpMessageHandler(s => _testIdentityServer.CreateHandler());
                           });
                 });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Do repeated calls to AddHttpClient overwrite each other?
But I am not sure if this means that the previous entry was overwritten. Can the same named client be "added" separately? Or...
Read more >
HttpClient sends the same request twice in some cases
Description. After these steps HttpClient tries to re-establish TCP connection to server and sends the same request again. or (if netcat starts ...
Read more >
Adding Resilience and Transient Fault handling to your . ...
Core 2.1 I moved my Http Client instances over to be created by the new HttpClientFactory. Now I have a single central place...
Read more >
Handle errors in ASP.NET Core web APIs
Learn about error handling with ASP.NET Core web APIs.
Read more >
Deserialise Json and Read Stream twice in C# - ...
I was think a lot that should I write this answer or not. I don't want to be rude or offensive, but this...
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