How to pass/inject more than one HttpClient parameter to a typed HttpClientClass?Finalize/Dispose pattern in C#Pass Method as Parameter using C#How do you set the Content-Type header for an HttpClient request?How to handle page not found by HttpClient?How to “refresh” cookie returned with HttpClientHttpClient single instance with different authentication headersHow to inject a 'register typed' parameter in a constructorHow to register a Typed HttpClient in a ASP.NET Core 2.1 applicationHow to resolve service and inject additional constructor parameters at runtime using .NET Core dependency injection?Cannot inject HttpClient in typed client while using with IMediatR library
What prevents a US state from colonizing a smaller state?
Why is exile often an intermediate step?
Is it theoretically possible to hack printer using scanner tray?
Is leaving out prefixes like "rauf", "rüber", "rein" when describing movement considered a big mistake in spoken German?
Correct use of the the idiom 'Гнать/Катить бочку'
Can dual citizens open crypto exchange accounts where U.S. citizens are prohibited?
Does friction always oppose motion?
Why didn't Caesar move against Sextus Pompey immediately after Munda?
Avoiding repetition when using the "snprintf idiom" to write text
Why do some PCBs have exposed plated perimeters?
Where can I find my serialized Sitecore items?
Should Catholics in a state of grace call themselves sinners?
How does mmorpg store data?
I just started; should I accept a farewell lunch for a coworker I don't know?
How does the 'five minute adventuring day' affect class balance?
Meaning of the word "good" in context
Hard for me to understand one tip written in "The as-if rule" of cppreference
Is there a list of all of the cases in the Talmud where תיקו ("Teiku") is said?
What does 'in attendance' mean on a death certificate - England?
Copy group of files (Filename*) to backup (Filename*.bak)
Russian equivalents of 能骗就骗 (if you can cheat, then cheat)
How do I tell my girlfriend she's been buying me books by the wrong author for the last nine months?
Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?
Listen to my Story...Let us find the Unique Invisible Pan Digital Pair
How to pass/inject more than one HttpClient parameter to a typed HttpClientClass?
Finalize/Dispose pattern in C#Pass Method as Parameter using C#How do you set the Content-Type header for an HttpClient request?How to handle page not found by HttpClient?How to “refresh” cookie returned with HttpClientHttpClient single instance with different authentication headersHow to inject a 'register typed' parameter in a constructorHow to register a Typed HttpClient in a ASP.NET Core 2.1 applicationHow to resolve service and inject additional constructor parameters at runtime using .NET Core dependency injection?Cannot inject HttpClient in typed client while using with IMediatR library
I want to register a typed HttpClient as here Microsoft docs.
Basically, the approach should be
services.AddHttpClient();
normally the pattern of these classes receive only the HttpClient class as a parameter and you implement the logic to call the endpoint. In my case, I need to use 2 HttpClient inside my MyHttpClient, one that pings the endpoint and the other one that talks with an IdentityProvider to discover the refreshEndpoints to refresh my cookies.
public class MyHttpClient : IMyHttpClient
public MyHttpClient (HttpClient httpClient,
HttpClient refreshHttpClient)
If I am trying to resolve from a controller an IMyHttpClient, I get an error saying it can't resolve an HttpClient.
In the GitHub code on line 43 AddHttpClient you can see that is calling
DefaultTypedHttpClientFactory.
If you go to the implementation of the DefaultTypedHttpClientFactory implementation you will notice that is a generic type. And when it calls CreateClient it only passes one parameter to the constructor on line 39.
The only workaround I am seeing here is to not create a typed client and register a normal class that receives an IHttpClientFactory and create and configure my clients on the fly, not as typed.
Any other idea?
c# asp.net-core dependency-injection .net-core dotnet-httpclient
|
show 6 more comments
I want to register a typed HttpClient as here Microsoft docs.
Basically, the approach should be
services.AddHttpClient();
normally the pattern of these classes receive only the HttpClient class as a parameter and you implement the logic to call the endpoint. In my case, I need to use 2 HttpClient inside my MyHttpClient, one that pings the endpoint and the other one that talks with an IdentityProvider to discover the refreshEndpoints to refresh my cookies.
public class MyHttpClient : IMyHttpClient
public MyHttpClient (HttpClient httpClient,
HttpClient refreshHttpClient)
If I am trying to resolve from a controller an IMyHttpClient, I get an error saying it can't resolve an HttpClient.
In the GitHub code on line 43 AddHttpClient you can see that is calling
DefaultTypedHttpClientFactory.
If you go to the implementation of the DefaultTypedHttpClientFactory implementation you will notice that is a generic type. And when it calls CreateClient it only passes one parameter to the constructor on line 39.
The only workaround I am seeing here is to not create a typed client and register a normal class that receives an IHttpClientFactory and create and configure my clients on the fly, not as typed.
Any other idea?
c# asp.net-core dependency-injection .net-core dotnet-httpclient
why have 2? could you hide the one behind another typed client?
– Daniel A. White
Mar 25 at 15:51
if I do that, then I need to create a regular class that receives both client implementations to orchestrate everything I need there. Something I would like to do it in one. In my scenario, I would like to make a call to an endpoint, but before doing that I would like to see if I need to refresh a token, which needs to be done by another http channel call
– Zinov
Mar 25 at 15:57
FWIW, this breaks SRP (single-responsibility principle). There's two different sets of logic here, which screams for two different classes. I'd actually even say three classes. One to work with your actualHttpClient
, one to do the refresh stuff, and then a third to orchestrate the two. The third would be the one you'd actually inject in places like controllers.
– Chris Pratt
Mar 25 at 16:05
How arehttpClient
andrefreshHttpClient
different, exactly? Not clear on why you need two. TheHttpClient
class is thread-safe so you ought to be able to use it both for the ping and for the discovery, unless there is some difference.
– John Wu
Mar 25 at 16:08
@JhonWu the discovery endpoint is different from the endpoint the HttpClient should do
– Zinov
Mar 25 at 16:10
|
show 6 more comments
I want to register a typed HttpClient as here Microsoft docs.
Basically, the approach should be
services.AddHttpClient();
normally the pattern of these classes receive only the HttpClient class as a parameter and you implement the logic to call the endpoint. In my case, I need to use 2 HttpClient inside my MyHttpClient, one that pings the endpoint and the other one that talks with an IdentityProvider to discover the refreshEndpoints to refresh my cookies.
public class MyHttpClient : IMyHttpClient
public MyHttpClient (HttpClient httpClient,
HttpClient refreshHttpClient)
If I am trying to resolve from a controller an IMyHttpClient, I get an error saying it can't resolve an HttpClient.
In the GitHub code on line 43 AddHttpClient you can see that is calling
DefaultTypedHttpClientFactory.
If you go to the implementation of the DefaultTypedHttpClientFactory implementation you will notice that is a generic type. And when it calls CreateClient it only passes one parameter to the constructor on line 39.
The only workaround I am seeing here is to not create a typed client and register a normal class that receives an IHttpClientFactory and create and configure my clients on the fly, not as typed.
Any other idea?
c# asp.net-core dependency-injection .net-core dotnet-httpclient
I want to register a typed HttpClient as here Microsoft docs.
Basically, the approach should be
services.AddHttpClient();
normally the pattern of these classes receive only the HttpClient class as a parameter and you implement the logic to call the endpoint. In my case, I need to use 2 HttpClient inside my MyHttpClient, one that pings the endpoint and the other one that talks with an IdentityProvider to discover the refreshEndpoints to refresh my cookies.
public class MyHttpClient : IMyHttpClient
public MyHttpClient (HttpClient httpClient,
HttpClient refreshHttpClient)
If I am trying to resolve from a controller an IMyHttpClient, I get an error saying it can't resolve an HttpClient.
In the GitHub code on line 43 AddHttpClient you can see that is calling
DefaultTypedHttpClientFactory.
If you go to the implementation of the DefaultTypedHttpClientFactory implementation you will notice that is a generic type. And when it calls CreateClient it only passes one parameter to the constructor on line 39.
The only workaround I am seeing here is to not create a typed client and register a normal class that receives an IHttpClientFactory and create and configure my clients on the fly, not as typed.
Any other idea?
c# asp.net-core dependency-injection .net-core dotnet-httpclient
c# asp.net-core dependency-injection .net-core dotnet-httpclient
asked Mar 25 at 15:48
ZinovZinov
1,8401 gold badge16 silver badges41 bronze badges
1,8401 gold badge16 silver badges41 bronze badges
why have 2? could you hide the one behind another typed client?
– Daniel A. White
Mar 25 at 15:51
if I do that, then I need to create a regular class that receives both client implementations to orchestrate everything I need there. Something I would like to do it in one. In my scenario, I would like to make a call to an endpoint, but before doing that I would like to see if I need to refresh a token, which needs to be done by another http channel call
– Zinov
Mar 25 at 15:57
FWIW, this breaks SRP (single-responsibility principle). There's two different sets of logic here, which screams for two different classes. I'd actually even say three classes. One to work with your actualHttpClient
, one to do the refresh stuff, and then a third to orchestrate the two. The third would be the one you'd actually inject in places like controllers.
– Chris Pratt
Mar 25 at 16:05
How arehttpClient
andrefreshHttpClient
different, exactly? Not clear on why you need two. TheHttpClient
class is thread-safe so you ought to be able to use it both for the ping and for the discovery, unless there is some difference.
– John Wu
Mar 25 at 16:08
@JhonWu the discovery endpoint is different from the endpoint the HttpClient should do
– Zinov
Mar 25 at 16:10
|
show 6 more comments
why have 2? could you hide the one behind another typed client?
– Daniel A. White
Mar 25 at 15:51
if I do that, then I need to create a regular class that receives both client implementations to orchestrate everything I need there. Something I would like to do it in one. In my scenario, I would like to make a call to an endpoint, but before doing that I would like to see if I need to refresh a token, which needs to be done by another http channel call
– Zinov
Mar 25 at 15:57
FWIW, this breaks SRP (single-responsibility principle). There's two different sets of logic here, which screams for two different classes. I'd actually even say three classes. One to work with your actualHttpClient
, one to do the refresh stuff, and then a third to orchestrate the two. The third would be the one you'd actually inject in places like controllers.
– Chris Pratt
Mar 25 at 16:05
How arehttpClient
andrefreshHttpClient
different, exactly? Not clear on why you need two. TheHttpClient
class is thread-safe so you ought to be able to use it both for the ping and for the discovery, unless there is some difference.
– John Wu
Mar 25 at 16:08
@JhonWu the discovery endpoint is different from the endpoint the HttpClient should do
– Zinov
Mar 25 at 16:10
why have 2? could you hide the one behind another typed client?
– Daniel A. White
Mar 25 at 15:51
why have 2? could you hide the one behind another typed client?
– Daniel A. White
Mar 25 at 15:51
if I do that, then I need to create a regular class that receives both client implementations to orchestrate everything I need there. Something I would like to do it in one. In my scenario, I would like to make a call to an endpoint, but before doing that I would like to see if I need to refresh a token, which needs to be done by another http channel call
– Zinov
Mar 25 at 15:57
if I do that, then I need to create a regular class that receives both client implementations to orchestrate everything I need there. Something I would like to do it in one. In my scenario, I would like to make a call to an endpoint, but before doing that I would like to see if I need to refresh a token, which needs to be done by another http channel call
– Zinov
Mar 25 at 15:57
FWIW, this breaks SRP (single-responsibility principle). There's two different sets of logic here, which screams for two different classes. I'd actually even say three classes. One to work with your actual
HttpClient
, one to do the refresh stuff, and then a third to orchestrate the two. The third would be the one you'd actually inject in places like controllers.– Chris Pratt
Mar 25 at 16:05
FWIW, this breaks SRP (single-responsibility principle). There's two different sets of logic here, which screams for two different classes. I'd actually even say three classes. One to work with your actual
HttpClient
, one to do the refresh stuff, and then a third to orchestrate the two. The third would be the one you'd actually inject in places like controllers.– Chris Pratt
Mar 25 at 16:05
How are
httpClient
and refreshHttpClient
different, exactly? Not clear on why you need two. The HttpClient
class is thread-safe so you ought to be able to use it both for the ping and for the discovery, unless there is some difference.– John Wu
Mar 25 at 16:08
How are
httpClient
and refreshHttpClient
different, exactly? Not clear on why you need two. The HttpClient
class is thread-safe so you ought to be able to use it both for the ping and for the discovery, unless there is some difference.– John Wu
Mar 25 at 16:08
@JhonWu the discovery endpoint is different from the endpoint the HttpClient should do
– Zinov
Mar 25 at 16:10
@JhonWu the discovery endpoint is different from the endpoint the HttpClient should do
– Zinov
Mar 25 at 16:10
|
show 6 more comments
1 Answer
1
active
oldest
votes
You can't. You'll either need to inject another service layer or IHttpClientFactory
directly
Another service
public class MyRefreshClient
private readonly HttpClient _httpClient;
public MyRefreshClient(HttpClient httpClient)
_httpClient = httpClient;
...
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly MyRefreshClient _refreshClient;
public MyHttpClient(HttpClient httpClient, MyRefreshClient refreshClient)
_httpClient = httpClient;
_refreshClient = refreshClient;
Then:
services.AddHttpClient<MyRefreshClient>(c => ... );
services.AddHttpClient<MyHttpClient>(c => ... );
Inject IHttpClientFactory
(and use named clients):
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly HttpClient _refreshClient;
public MyHttpClient(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("MyHttpClient");
_refreshClient = httpClientFactory.CreateClient("MyRefreshClient");
Then:
services.AddHttpClient("MyHttpClient", c => ... );
services.AddHttpClient("MyRefreshClient", c=> ... );
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55341613%2fhow-to-pass-inject-more-than-one-httpclient-parameter-to-a-typed-httpclientclass%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't. You'll either need to inject another service layer or IHttpClientFactory
directly
Another service
public class MyRefreshClient
private readonly HttpClient _httpClient;
public MyRefreshClient(HttpClient httpClient)
_httpClient = httpClient;
...
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly MyRefreshClient _refreshClient;
public MyHttpClient(HttpClient httpClient, MyRefreshClient refreshClient)
_httpClient = httpClient;
_refreshClient = refreshClient;
Then:
services.AddHttpClient<MyRefreshClient>(c => ... );
services.AddHttpClient<MyHttpClient>(c => ... );
Inject IHttpClientFactory
(and use named clients):
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly HttpClient _refreshClient;
public MyHttpClient(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("MyHttpClient");
_refreshClient = httpClientFactory.CreateClient("MyRefreshClient");
Then:
services.AddHttpClient("MyHttpClient", c => ... );
services.AddHttpClient("MyRefreshClient", c=> ... );
add a comment |
You can't. You'll either need to inject another service layer or IHttpClientFactory
directly
Another service
public class MyRefreshClient
private readonly HttpClient _httpClient;
public MyRefreshClient(HttpClient httpClient)
_httpClient = httpClient;
...
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly MyRefreshClient _refreshClient;
public MyHttpClient(HttpClient httpClient, MyRefreshClient refreshClient)
_httpClient = httpClient;
_refreshClient = refreshClient;
Then:
services.AddHttpClient<MyRefreshClient>(c => ... );
services.AddHttpClient<MyHttpClient>(c => ... );
Inject IHttpClientFactory
(and use named clients):
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly HttpClient _refreshClient;
public MyHttpClient(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("MyHttpClient");
_refreshClient = httpClientFactory.CreateClient("MyRefreshClient");
Then:
services.AddHttpClient("MyHttpClient", c => ... );
services.AddHttpClient("MyRefreshClient", c=> ... );
add a comment |
You can't. You'll either need to inject another service layer or IHttpClientFactory
directly
Another service
public class MyRefreshClient
private readonly HttpClient _httpClient;
public MyRefreshClient(HttpClient httpClient)
_httpClient = httpClient;
...
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly MyRefreshClient _refreshClient;
public MyHttpClient(HttpClient httpClient, MyRefreshClient refreshClient)
_httpClient = httpClient;
_refreshClient = refreshClient;
Then:
services.AddHttpClient<MyRefreshClient>(c => ... );
services.AddHttpClient<MyHttpClient>(c => ... );
Inject IHttpClientFactory
(and use named clients):
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly HttpClient _refreshClient;
public MyHttpClient(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("MyHttpClient");
_refreshClient = httpClientFactory.CreateClient("MyRefreshClient");
Then:
services.AddHttpClient("MyHttpClient", c => ... );
services.AddHttpClient("MyRefreshClient", c=> ... );
You can't. You'll either need to inject another service layer or IHttpClientFactory
directly
Another service
public class MyRefreshClient
private readonly HttpClient _httpClient;
public MyRefreshClient(HttpClient httpClient)
_httpClient = httpClient;
...
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly MyRefreshClient _refreshClient;
public MyHttpClient(HttpClient httpClient, MyRefreshClient refreshClient)
_httpClient = httpClient;
_refreshClient = refreshClient;
Then:
services.AddHttpClient<MyRefreshClient>(c => ... );
services.AddHttpClient<MyHttpClient>(c => ... );
Inject IHttpClientFactory
(and use named clients):
public class MyHttpClient : IMyHttpClient
private readonly HttpClient _httpClient;
private readonly HttpClient _refreshClient;
public MyHttpClient(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("MyHttpClient");
_refreshClient = httpClientFactory.CreateClient("MyRefreshClient");
Then:
services.AddHttpClient("MyHttpClient", c => ... );
services.AddHttpClient("MyRefreshClient", c=> ... );
answered Mar 25 at 15:58
Chris PrattChris Pratt
166k22 gold badges255 silver badges320 bronze badges
166k22 gold badges255 silver badges320 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55341613%2fhow-to-pass-inject-more-than-one-httpclient-parameter-to-a-typed-httpclientclass%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why have 2? could you hide the one behind another typed client?
– Daniel A. White
Mar 25 at 15:51
if I do that, then I need to create a regular class that receives both client implementations to orchestrate everything I need there. Something I would like to do it in one. In my scenario, I would like to make a call to an endpoint, but before doing that I would like to see if I need to refresh a token, which needs to be done by another http channel call
– Zinov
Mar 25 at 15:57
FWIW, this breaks SRP (single-responsibility principle). There's two different sets of logic here, which screams for two different classes. I'd actually even say three classes. One to work with your actual
HttpClient
, one to do the refresh stuff, and then a third to orchestrate the two. The third would be the one you'd actually inject in places like controllers.– Chris Pratt
Mar 25 at 16:05
How are
httpClient
andrefreshHttpClient
different, exactly? Not clear on why you need two. TheHttpClient
class is thread-safe so you ought to be able to use it both for the ping and for the discovery, unless there is some difference.– John Wu
Mar 25 at 16:08
@JhonWu the discovery endpoint is different from the endpoint the HttpClient should do
– Zinov
Mar 25 at 16:10