Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure HttpClient via Unity container?

I'm trying to register an instance of HttpClient object with the unity container so that it can be used throughout the app, but running into the error - "The type HttpMessageHandler does not have an accessible constructor."

Here is the code I use to register the HttpClient with Unity-

private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        container.RegisterType<HttpClient>(
            new InjectionProperty("BaseAddress", new Uri(ConfigurationManager.AppSettings["ApiUrl"]))); 

        return container;
    }
like image 975
camelCaseWarrior Avatar asked Feb 04 '13 22:02

camelCaseWarrior


1 Answers

You can use the factory method to register it instead:

container.RegisterType<HttpClient>(
    new InjectionFactory(x => 
        new HttpClient { BaseAddress = new Uri(ConfigurationManager.AppSettings["ApiUrl"]) }
    )
); 
like image 179
jgauffin Avatar answered Sep 22 '22 03:09

jgauffin