Defining an 'HttpClient' singleton via StructureMap causes an error about 'HttpMessageHandler' being not...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Trying to define an HttpClient singleton in StructureMap ala:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();


This results in the following error in runtime (upon dependency injection):



   StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'System.Net.Http.HttpMessageHandler'

There is no configuration specified for System.Net.Http.HttpMessageHandler

1.) new HttpClient(*Default of HttpMessageHandler*)
2.) System.Net.Http.HttpClient
3.) Instance of System.Net.Http.HttpClient
4.) new AdmanAdapter(*Default of HttpClient*)
5.) Organotiki.vNext.PostEval.Data.Adapters.ADMAN.AdmanAdapter
6.) Instance of [....]

at lambda_method(Closure , IBuildSession , IContext )
at StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
at StructureMap.BuildSession.BuildNewInSession(Type pluginType, Instance instance)
at StructureMap.Pipeline.NulloTransientCache.Get(Type pluginType, Instance instance, IBuildSession session)
at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance)
at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)


If we also configure HttpMessageHandler like so:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();
For<HttpMessageHandler>().UseIfNone(x => new HttpClientHandler());


Then the problem goes away. The question is why? The default constructor for HttpClient takes care of its own dependency injection:



/// <summary>Initializes a new instance of the <see cref="T:System.Net.Http.HttpClient" /> class.</summary>
[__DynamicallyInvokable]
public HttpClient()
: this((HttpMessageHandler) new HttpClientHandler())
{
}


Am I missing something here?










share|improve this question























  • The container does not use the default constructor by default. It uses the constructor with the most dependencies if multiple constructors are found.

    – Nkosi
    Jan 3 at 15:43


















1















Trying to define an HttpClient singleton in StructureMap ala:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();


This results in the following error in runtime (upon dependency injection):



   StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'System.Net.Http.HttpMessageHandler'

There is no configuration specified for System.Net.Http.HttpMessageHandler

1.) new HttpClient(*Default of HttpMessageHandler*)
2.) System.Net.Http.HttpClient
3.) Instance of System.Net.Http.HttpClient
4.) new AdmanAdapter(*Default of HttpClient*)
5.) Organotiki.vNext.PostEval.Data.Adapters.ADMAN.AdmanAdapter
6.) Instance of [....]

at lambda_method(Closure , IBuildSession , IContext )
at StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
at StructureMap.BuildSession.BuildNewInSession(Type pluginType, Instance instance)
at StructureMap.Pipeline.NulloTransientCache.Get(Type pluginType, Instance instance, IBuildSession session)
at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance)
at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)


If we also configure HttpMessageHandler like so:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();
For<HttpMessageHandler>().UseIfNone(x => new HttpClientHandler());


Then the problem goes away. The question is why? The default constructor for HttpClient takes care of its own dependency injection:



/// <summary>Initializes a new instance of the <see cref="T:System.Net.Http.HttpClient" /> class.</summary>
[__DynamicallyInvokable]
public HttpClient()
: this((HttpMessageHandler) new HttpClientHandler())
{
}


Am I missing something here?










share|improve this question























  • The container does not use the default constructor by default. It uses the constructor with the most dependencies if multiple constructors are found.

    – Nkosi
    Jan 3 at 15:43














1












1








1








Trying to define an HttpClient singleton in StructureMap ala:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();


This results in the following error in runtime (upon dependency injection):



   StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'System.Net.Http.HttpMessageHandler'

There is no configuration specified for System.Net.Http.HttpMessageHandler

1.) new HttpClient(*Default of HttpMessageHandler*)
2.) System.Net.Http.HttpClient
3.) Instance of System.Net.Http.HttpClient
4.) new AdmanAdapter(*Default of HttpClient*)
5.) Organotiki.vNext.PostEval.Data.Adapters.ADMAN.AdmanAdapter
6.) Instance of [....]

at lambda_method(Closure , IBuildSession , IContext )
at StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
at StructureMap.BuildSession.BuildNewInSession(Type pluginType, Instance instance)
at StructureMap.Pipeline.NulloTransientCache.Get(Type pluginType, Instance instance, IBuildSession session)
at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance)
at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)


If we also configure HttpMessageHandler like so:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();
For<HttpMessageHandler>().UseIfNone(x => new HttpClientHandler());


Then the problem goes away. The question is why? The default constructor for HttpClient takes care of its own dependency injection:



/// <summary>Initializes a new instance of the <see cref="T:System.Net.Http.HttpClient" /> class.</summary>
[__DynamicallyInvokable]
public HttpClient()
: this((HttpMessageHandler) new HttpClientHandler())
{
}


Am I missing something here?










share|improve this question














Trying to define an HttpClient singleton in StructureMap ala:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();


This results in the following error in runtime (upon dependency injection):



   StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'System.Net.Http.HttpMessageHandler'

There is no configuration specified for System.Net.Http.HttpMessageHandler

1.) new HttpClient(*Default of HttpMessageHandler*)
2.) System.Net.Http.HttpClient
3.) Instance of System.Net.Http.HttpClient
4.) new AdmanAdapter(*Default of HttpClient*)
5.) Organotiki.vNext.PostEval.Data.Adapters.ADMAN.AdmanAdapter
6.) Instance of [....]

at lambda_method(Closure , IBuildSession , IContext )
at StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
at StructureMap.BuildSession.BuildNewInSession(Type pluginType, Instance instance)
at StructureMap.Pipeline.NulloTransientCache.Get(Type pluginType, Instance instance, IBuildSession session)
at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance)
at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)


If we also configure HttpMessageHandler like so:



For<HttpClient>().Singleton().UseIfNone<HttpClient>();
For<HttpMessageHandler>().UseIfNone(x => new HttpClientHandler());


Then the problem goes away. The question is why? The default constructor for HttpClient takes care of its own dependency injection:



/// <summary>Initializes a new instance of the <see cref="T:System.Net.Http.HttpClient" /> class.</summary>
[__DynamicallyInvokable]
public HttpClient()
: this((HttpMessageHandler) new HttpClientHandler())
{
}


Am I missing something here?







c# rest dependency-injection structuremap dotnet-httpclient






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 14:55









XDSXDS

1,46611635




1,46611635













  • The container does not use the default constructor by default. It uses the constructor with the most dependencies if multiple constructors are found.

    – Nkosi
    Jan 3 at 15:43



















  • The container does not use the default constructor by default. It uses the constructor with the most dependencies if multiple constructors are found.

    – Nkosi
    Jan 3 at 15:43

















The container does not use the default constructor by default. It uses the constructor with the most dependencies if multiple constructors are found.

– Nkosi
Jan 3 at 15:43





The container does not use the default constructor by default. It uses the constructor with the most dependencies if multiple constructors are found.

– Nkosi
Jan 3 at 15:43












2 Answers
2






active

oldest

votes


















2














From structuremap docs at http://structuremap.github.io/registration/constructor-selection




If there are multiple public constructor functions on a concrete
class, StructureMap's default behavior is to select the "greediest"
constructor, i.e., the constructor function with the most parameters.




If you look at the possible constructors for HttpClient, it should be



public HttpClient();
public HttpClient(HttpMessageHandler handler);
public HttpClient(HttpMessageHandler handler, bool disposeHandler);





share|improve this answer
























  • Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

    – XDS
    Jan 3 at 15:47






  • 1





    @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

    – Brad M
    Jan 3 at 15:53













  • I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

    – XDS
    Jan 3 at 16:16






  • 1





    You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

    – Brad M
    Jan 3 at 16:21





















1














Extending @Brad M's answer, what worked for me is .SelectConstructor(() => new HttpClient()). Specifying which constructor should be used explicitly.






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%2f54024716%2fdefining-an-httpclient-singleton-via-structuremap-causes-an-error-about-httpm%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    From structuremap docs at http://structuremap.github.io/registration/constructor-selection




    If there are multiple public constructor functions on a concrete
    class, StructureMap's default behavior is to select the "greediest"
    constructor, i.e., the constructor function with the most parameters.




    If you look at the possible constructors for HttpClient, it should be



    public HttpClient();
    public HttpClient(HttpMessageHandler handler);
    public HttpClient(HttpMessageHandler handler, bool disposeHandler);





    share|improve this answer
























    • Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

      – XDS
      Jan 3 at 15:47






    • 1





      @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

      – Brad M
      Jan 3 at 15:53













    • I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

      – XDS
      Jan 3 at 16:16






    • 1





      You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

      – Brad M
      Jan 3 at 16:21


















    2














    From structuremap docs at http://structuremap.github.io/registration/constructor-selection




    If there are multiple public constructor functions on a concrete
    class, StructureMap's default behavior is to select the "greediest"
    constructor, i.e., the constructor function with the most parameters.




    If you look at the possible constructors for HttpClient, it should be



    public HttpClient();
    public HttpClient(HttpMessageHandler handler);
    public HttpClient(HttpMessageHandler handler, bool disposeHandler);





    share|improve this answer
























    • Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

      – XDS
      Jan 3 at 15:47






    • 1





      @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

      – Brad M
      Jan 3 at 15:53













    • I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

      – XDS
      Jan 3 at 16:16






    • 1





      You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

      – Brad M
      Jan 3 at 16:21
















    2












    2








    2







    From structuremap docs at http://structuremap.github.io/registration/constructor-selection




    If there are multiple public constructor functions on a concrete
    class, StructureMap's default behavior is to select the "greediest"
    constructor, i.e., the constructor function with the most parameters.




    If you look at the possible constructors for HttpClient, it should be



    public HttpClient();
    public HttpClient(HttpMessageHandler handler);
    public HttpClient(HttpMessageHandler handler, bool disposeHandler);





    share|improve this answer













    From structuremap docs at http://structuremap.github.io/registration/constructor-selection




    If there are multiple public constructor functions on a concrete
    class, StructureMap's default behavior is to select the "greediest"
    constructor, i.e., the constructor function with the most parameters.




    If you look at the possible constructors for HttpClient, it should be



    public HttpClient();
    public HttpClient(HttpMessageHandler handler);
    public HttpClient(HttpMessageHandler handler, bool disposeHandler);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 15:44









    Brad MBrad M

    7,16911633




    7,16911633













    • Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

      – XDS
      Jan 3 at 15:47






    • 1





      @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

      – Brad M
      Jan 3 at 15:53













    • I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

      – XDS
      Jan 3 at 16:16






    • 1





      You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

      – Brad M
      Jan 3 at 16:21





















    • Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

      – XDS
      Jan 3 at 15:47






    • 1





      @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

      – Brad M
      Jan 3 at 15:53













    • I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

      – XDS
      Jan 3 at 16:16






    • 1





      You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

      – Brad M
      Jan 3 at 16:21



















    Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

    – XDS
    Jan 3 at 15:47





    Thanks for the insight. Just one more question: If I use "For<HttpClient>().Singleton().UseIfNone(x => new HttpClient());" I still get the same error despite the fact that I have specified the constructor explicitly. What gives?

    – XDS
    Jan 3 at 15:47




    1




    1





    @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

    – Brad M
    Jan 3 at 15:53







    @XDS That doesn't look correct. You are explicitly saying, "for this concrete class, use a single instance of it. If no concrete instance is found, then use this lambda/constructor logic". HttpClient is a concrete class, so structuremap is always able to "attempt" to create an instance of it. If it was an interface for example, IFoo, then it would resort to using your lamdba expression to get an instance. As it is now, it just completely ignores your UseIfNone.

    – Brad M
    Jan 3 at 15:53















    I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

    – XDS
    Jan 3 at 16:16





    I guess I have to subclass HttpClient then decorate the subclass with an interface and then configure that one instead. What a strange trip this will be. Thanks btw. I could have never figured all these subtleties on my own. Thumbs up mate.

    – XDS
    Jan 3 at 16:16




    1




    1





    You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

    – Brad M
    Jan 3 at 16:21







    You should be able to do something similar to the following... For<HttpClient>().Singleton().Use(x => new HttpClient()). Here you are explicitly telling structuremap "This is exactly how I want the HttpClient instance to be created"

    – Brad M
    Jan 3 at 16:21















    1














    Extending @Brad M's answer, what worked for me is .SelectConstructor(() => new HttpClient()). Specifying which constructor should be used explicitly.






    share|improve this answer






























      1














      Extending @Brad M's answer, what worked for me is .SelectConstructor(() => new HttpClient()). Specifying which constructor should be used explicitly.






      share|improve this answer




























        1












        1








        1







        Extending @Brad M's answer, what worked for me is .SelectConstructor(() => new HttpClient()). Specifying which constructor should be used explicitly.






        share|improve this answer















        Extending @Brad M's answer, what worked for me is .SelectConstructor(() => new HttpClient()). Specifying which constructor should be used explicitly.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 18 at 17:51

























        answered Mar 18 at 17:31









        NikhileshNikhilesh

        1,04421117




        1,04421117






























            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%2f54024716%2fdefining-an-httpclient-singleton-via-structuremap-causes-an-error-about-httpm%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

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory