Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for some servicing-proposed HttpClient Factory issues #2710

Merged
merged 4 commits into from
Jan 16, 2020

Conversation

rynowak
Copy link
Member

@rynowak rynowak commented 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

dotnet/aspnetcore#13346 Attempting to call AddTypedClient<T> twice on the same builder with different types throws an exception. This is a valuable shorthand way of expressing a configuration once and then binding it to multiple types.

#2077 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.

@bladefist
Copy link

Looks like this didn't make it into 3.1, is it coming soon?

@rynowak rynowak added the servicing-consider Shiproom approval is required for the issue label Dec 5, 2019
@jamshedd jamshedd added servicing-approved and removed servicing-consider Shiproom approval is required for the issue labels Dec 10, 2019
@jamshedd jamshedd added this to the 3.1.1 milestone Dec 10, 2019
@VinceEeckhout
Copy link

Any news when 3.1.1 might be released?

@mkArtakMSFT
Copy link
Member

These were approved for 3.1.2, so updating the milestone accordingly /cc @jamshedd

@johnkors
Copy link

johnkors commented Dec 19, 2019

@mkArtakMSFT, Will the fix in 3.1.2 also work for the combo of typed+named clients?

services.AddHttpClient<MyHttpClient>("client1", config.GetSection("client1config"));
services.AddHttpClient<MyHttpClient>("client2", config.GetSection("client2config"));
services.AddHttpClient<MyHttpClient>("client3", config.GetSection("client3config"));

var httpClient = _clientFactory.CreateClient("client1"); // MyHttpClient using client1config

@VinceEeckhout
Copy link

VinceEeckhout commented Dec 19, 2019 via email

@lwansbrough
Copy link

Proposal: Add validation to the validation to prevent mistakes.

@vivmishra vivmishra modified the milestones: 3.1.2, 3.1.3 Jan 9, 2020
@vivmishra
Copy link

Moved to Mar as per Tactics. Will need to be explicitly approved for Feb, if required.

@Pilchie Pilchie modified the milestones: 3.1.3, 3.1.2 Jan 15, 2020
@Pilchie
Copy link
Member

Pilchie commented Jan 15, 2020

Pulling back into 3.1.2.

@Pilchie
Copy link
Member

Pilchie commented Jan 15, 2020

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Pilchie
Copy link
Member

Pilchie commented Jan 15, 2020

@mkArtakMSFT @rynowak - can we get someone to review this and get it merged?

@dougbu
Copy link
Member

dougbu commented Jan 16, 2020

Has this been reviewed offline? If it's ready, please someone to sign off then get it in before 4PM today 😃

@johnkors
Copy link

johnkors commented Jan 16, 2020

Is there a preview-nuget we can install to test this?

I did not get a response on this comment, which works in 2.1, but is a regression in 3.0/3.1.

Reading the commits, it still looks like there is validation around validateSingleType - which I would assume breaks what I mentioned in the comment

Fixes: #2077

In 3.0 we introduced validation to try and prevent some cases of invalid
usage on the factory that had lead to user bug reports.

Unfortunately we blocked a few legitimate usage scenarios behind
expections.

In this case the common usage is for a library to register a typed
client with `AddHttpClient<MyClient>(...)`. User code can then
collaborate by calling the same thing, and interacting with the builder
that's returned.

This change explicitly allows this pattern by fine-tuning the
validation.
Fixes: dotnet/aspnetcore#13346

In 3.0 we added validation to try and report exceptions for some common
HttClient factory usage mistakes in the registration code. Unfortunately
we blocked from legitimate usage cases.

In this case, users are blocked from associating multiple types with the
same logical 'name'.

Ex:

```C#
services.AddHttpClient("Foo").AddTypedClient<A>().AddTypedClient<B>();
```

This is useful and should be allowed because it's a good way to DRY up
configuration code.

----

This change relaxes the validation when `AddTypedClient` is called, but
not when `AddHttpClient` is called without supplying a name.

We still want to block cases like the following:

```C#
services.AddHttpClient<A.SomeClient>();
services.AddHttpClient<B.SomeClient>();
```

The type short name is used as the logical name for the client (named
options) so usage like this is always a bug.
Also reported as part of #2077

In this pattern users register many instances of the same client with
different configurations, and multiplex between them in a round-robin
fashion.
@rynowak rynowak force-pushed the rynowak/http-client-factory-fixes branch from 6b412ee to edcf5f2 Compare January 16, 2020 16:38
@mkArtakMSFT mkArtakMSFT merged commit 08a9b86 into release/3.1 Jan 16, 2020
@mkArtakMSFT mkArtakMSFT deleted the rynowak/http-client-factory-fixes branch January 16, 2020 17:55
@kjkrum
Copy link

kjkrum commented Aug 24, 2020

Has this change been released? Calling AddTypedClient<T> more than once on the same builder works in a debug build, but the same project crashes as described here when it's deployed in production.

I'm using Microsoft.Extensions.Hosting 3.1.7, if that's the relevant package.

@pranavkm
Copy link

Hi, it looks like you are posting on a closed issue/PR/commit!

We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

Thanks!

@dotnet dotnet locked as resolved and limited conversation to collaborators Aug 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet