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













0















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?










share|improve this question






















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















0















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?










share|improve this question






















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













0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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











  • @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











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
















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










1 Answer
1






active

oldest

votes


















1














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=> ... );





share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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=> ... );





    share|improve this answer



























      1














      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=> ... );





      share|improve this answer

























        1












        1








        1







        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=> ... );





        share|improve this answer













        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=> ... );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 15:58









        Chris PrattChris Pratt

        166k22 gold badges255 silver badges320 bronze badges




        166k22 gold badges255 silver badges320 bronze badges
















            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.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현