【发布时间】:2021-07-29 11:52:33
【问题描述】:
我的情况类似于下面的代码。鉴于在类的实例化发生时静态 HttpClient 属性已经存在,我无法注入 IConfiguration 或 IOptions,如何从 app.settings 获取代理 URI?
public class SomeApiClient : ISomeApiClient
{
private static readonly HttpClientHandler ProxyClientHandler = new HttpClientHandler
{
Proxy = new WebProxy("http://serviceproxy.com"), //TODO Fetch from settings
UseProxy = true,
};
//HttpClient is disposable but should be used liked this
//(https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/#how-to-fix-improper-instantiation-antipattern)
private static readonly HttpClient RestClient = new HttpClient(ProxyClientHandler);
//The rest of the non-static class goes here...
}
【问题讨论】:
-
ProxyClientHandler.Proxy 有一个setter 为什么不改变Constructor?
-
在构造函数中设置代理属性确实有效,但我认为“类型化客户端”是我正在寻找的。非常感谢你们的回复...
标签: c# .net-core configuration httpclient