I'm try to construct HttpClient before register it in unity, but it fails at runtime with error message says HttpMessageHandler not accessible.
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:3721");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
IUnityContainer container = new UnityContainer();
container.RegisterInstance<HttpClient>(client);
IUnityContainer newcontainer = new UnityContainer();
HttpClient newclient = newcontainer.Resolve<HttpClient>();
It seems unity create another HttpClient instance using the constructor which have the most arguments.
HttpClient(HttpMessageHandler handler, bool disposeHandler);
HttpMessageHandler is abstract class, so I think this the problem my code fails at runtime. So, How can I control unity to use which construct or is there a way to that unity use already constructed instance?
See Question&Answers more detail:os