IHTTPCLIENTFACTORY和HTTPCLIENT之间有什么区别
创建客户端对象
当我们使用var client
= new httpclient(); 以及使用ihttpclientfactory时 会发生什么情况 builder.services.addhttpclient();
并通过依赖注入使用它
public readonly IHttpClientFactory _clientFactory;
public RequestController(ClientPolicy clientPolicy,IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
var client= _clientFactory.CreateClient();
What happens when we create client object using
var client=new HttpClient();
And when we use IHttpClientFactorybuilder.Services.AddHttpClient();
and using it through Dependency Injection
public readonly IHttpClientFactory _clientFactory;
public RequestController(ClientPolicy clientPolicy,IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
var client= _clientFactory.CreateClient();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从上面的评论和文章中学到的答案是,当httpclient被处置了其插座时未置于该插座时,这就是为什么它会造成套接字耗尽问题,并且如果我们创建HTTPCLCCLIENT的Singleton对象,则会出现DNS错误,那就是原因。网络核心我们实现IHTTPCLIENTFACTORY,因此它可以克服套接字耗尽问题和DNS问题,并且在代码修改的情况下,在单个类中易于维护IhttpClientFactory
The Answer learnt from the above comment and articles is that when an HttpClient is Disposed its socket not disposed so soon that's why it create Socket Exhaustion problem and if we create singleton object of HttpClient then there will be DNS Error that's why in . Net core we implements IHttpClientFactory so it can overcome Socket Exhaustion problem and DNS Problem and its easy to maintain IHttpClientFactory at a single class in case of code modifications it will be implemented