我尝试在统一注册之前构建 HttpClient,但它在运行时失败,错误消息显示 HttpMessageHandler 无法访问。
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>();
似乎统一使用具有最多参数的构造函数创建了另一个 HttpClient 实例。
HttpClient(HttpMessageHandler handler, bool disposeHandler);
HttpMessageHandler 是抽象类,所以我认为这是我的代码在运行时失败的问题。那么,我如何控制统一使用哪个构造,或者有没有办法让统一使用已经构造的实例?