-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Configure SocketsHttpHandler for HttpClientFactory with IConfiguration #88864
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cfccf86
Implementation
CarnaViire 05d6ec1
Add tests
CarnaViire 89680f0
API review changes and minor code reshuffling
CarnaViire ea76239
Add triple slash docs
CarnaViire 24a70df
Merge remote-tracking branch 'upstream/main' into hcf-socketshttphandler
CarnaViire 93c035d
Merge remote-tracking branch 'upstream/main' into hcf-socketshttphandler
CarnaViire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ies/Microsoft.Extensions.Http/src/DependencyInjection/DefaultSocketsHttpHandlerBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#if NET5_0_OR_GREATER | ||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
internal sealed class DefaultSocketsHttpHandlerBuilder : ISocketsHttpHandlerBuilder | ||
{ | ||
public DefaultSocketsHttpHandlerBuilder(IServiceCollection services, string name) | ||
{ | ||
Services = services; | ||
Name = name; | ||
} | ||
|
||
public string Name { get; } | ||
|
||
public IServiceCollection Services { get; } | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...libraries/Microsoft.Extensions.Http/src/DependencyInjection/ISocketsHttpHandlerBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#if NET5_0_OR_GREATER | ||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
/// <summary> | ||
/// A builder for configuring <see cref="System.Net.Http.SocketsHttpHandler"/> for a named | ||
/// <see cref="System.Net.Http.HttpClient"/> instances returned by <see cref="System.Net.Http.IHttpClientFactory"/>. | ||
/// </summary> | ||
public interface ISocketsHttpHandlerBuilder | ||
{ | ||
/// <summary> | ||
/// Gets the name of the client for a handler configured by this builder. | ||
/// </summary> | ||
string Name { get; } | ||
|
||
/// <summary> | ||
/// Gets the application service collection. | ||
/// </summary> | ||
IServiceCollection Services { get; } | ||
} | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it a Use vs Add/Configure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because then it would have been ConfigurePrimaryHandlerToBeSocketsHttpHandler 😅
Point is, you don't have SocketsHttpHandler in HttpClientFactory by default - you have some PrimaryHandler, so if you'd say ConfigureSocketsHttpHandler, the question is -- where did it come from, so now we're configuring it?
So to me, saying UseSocketsHttpHandler was much clearer in explaining what happens - after you call it, the factory would be using SocketsHttpHandler in this named client (and you might decide not to configure it, if you don't specify a delegate)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConfigureSocketsHttpHandler
would be a better name. We don't have any other APIs that are Use at this level, I'm not sure why we'd introduce this verb when it's calling a Configure* method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree that "Use" is very rarely used, I don't think that
ConfigureSocketsHttpHandler
is better, for the reason I described aboveIt's calling ConfigurePrimaryHttpMessageHandler method. "Primary" is an important piece of information in relation to "Configure" verb, as Primary handler is part of HttpClientFactory's "concept", it's a "property" on an HttpClient's configuration.
Primary handler property is a "left-hand operand" in the assignment, and SocketsHttpHandler value is a "right-hand operand", so to say.
But that's my opinion; I've sent an email to the API review board to discuss this further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the example usage, I'm not sure I like "Configure..." given the nested calls to
Configure(
in the builder callback.@davidfowl During API review, I thought your feedback would be to have it return the
ISocketsHttpHandlerBuilder
to reduce nesting. But if we did that, "Use..." would feel more out of place. "Add..." would be far more conventional given things likeAddAuthentication
andAddMvc
, but it is a little weird that add really means replace in this case. But for completeness, the builder-returning API would look something like this:But as @CarnaViire pointed out in API review, this could be annoying if you need the
IHttpClientBuilder
for anything else, you'd need to store the builder in a variable unlessAddSocketsHttpHandler
is the last part of the chain, so we decided against it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use(this IWebHostBuilder) - Adding a combination of services, logging and configuration
Use(this IApplicationBuilder) - Adding middleware to the pipeline
Map(this IEndpointRouteBuilder) - Adding a route
Add(this IServiceCollection) - Adding services to the DI container
Configure(this IServiceCollecton) - Configuring options for T
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems there is still ongoing naming discussion.
Given the timelines, I asked @CarnaViire to check in current state and then leave potential name change for later once we have consensus.
That way, majority of the work will get into customers' hands in Preview7 and we have a chance to get a feedback. We can easily change the public API name afterwards as P7 is NOT go-live.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a problem with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CarnaViire can you open a follow up issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidfowl #89023 (comment)