当需要向某特定URL地址发送HTTP请求并得到相应响应时,通常会用到HttpClient类。该类包含了众多有用的方法,可以满足绝大多数的需求。但是如果对其使用不当时,可能会出现意想不到的事情。
using(var client = new HttpClient())
对象所占用资源应该确保及时被释放掉,但是,对于网络连接而言,这是错误的。


原因有二,网络连接是需要耗费一定时间的,频繁开启与关闭连接,性能会受影响;再者,开启网络连接时会占用底层socket资源,但在HttpClient调用其本身的Dispose方法时,并不能立刻释放该资源,这意味着你的程序可能会因为耗尽连接资源而产生预期之外的异常。

所以比较好的解决方法是延长HttpClient对象的使用寿命,比如对其建一个静态的对象:
private static HttpClient Client = new HttpClient();
但从程序员的角度来看,这样的代码或许不够优雅。

所以在.NET Core 2.1中引入了新的HttpClientFactory类。

它的用法很简单,首先是对其进行IoC的注册:
1 public void ConfigureServices(IServiceCollection services) 2 { 3
services.AddHttpClient();4 services.AddMvc(); 5 }
然后通过IHttpClientFactory创建一个HttpClient对象,之后的操作如旧,但不需要担心其内部资源的释放:
1 public class LzzDemoController : Controller 2 { 3 IHttpClientFactory
_httpClientFactory; 4 5 public LzzDemoController(IHttpClientFactory
httpClientFactory) 6 { 7 _httpClientFactory = httpClientFactory; 8 } 9 10
public IActionResult Index() 11 { 12 var client =
_httpClientFactory.CreateClient();13 var result = client.GetStringAsync("
http://myurl/"); 14 return View(); 15 } 16 }
AddHttpClient的源码:
1 public static IServiceCollection AddHttpClient(this IServiceCollection
services) 2 { 3 if (services == null) 4 { 5 throw new
ArgumentNullException(nameof(services)); 6 } 7 8 services.AddLogging(); 9
services.AddOptions();10 11 // 12 // Core abstractions 13 // 14
services.TryAddTransient<HttpMessageHandlerBuilder,
DefaultHttpMessageHandlerBuilder>(); 15
services.TryAddSingleton<IHttpClientFactory, DefaultHttpClientFactory>(); 16 17
// 18 // Typed Clients 19 // 20 services.TryAdd(ServiceDescriptor.Singleton(
typeof(ITypedHttpClientFactory<>), typeof(DefaultTypedHttpClientFactory<>))); 21
22 // 23 // Misc infrastructure 24 // 25
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHttpMessageHandlerBuilderFilter,
LoggingHttpMessageHandlerBuilderFilter>()); 26 27 return services; 28 }
它的内部为IHttpClientFactory接口绑定了DefaultHttpClientFactory类。

再看IHttpClientFactory接口中关键的CreateClient方法:
1 public HttpClient CreateClient(string name) 2 { 3 if (name == null) 4 {
5 throw new ArgumentNullException(nameof(name)); 6 } 7 8 var entry =
_activeHandlers.GetOrAdd(name, _entryFactory).Value; 9 var client = new
HttpClient(entry.Handler, disposeHandler:false); 10 11
StartHandlerEntryTimer(entry);12 13 var options = _optionsMonitor.Get(name); 14
for (var i = 0; i < options.HttpClientActions.Count; i++) 15 { 16
options.HttpClientActions[i](client);17 } 18 19 return client; 20 }
HttpClient的创建不再是简单的new HttpClient(),而是传入了两个参数:HttpMessageHandler handler与bool
disposeHandler。disposeHandler参数为false值时表示要重用内部的handler对象。handler参数则从上一句的代码可以看出是以name为键值从一字典中取出,又因为DefaultHttpClientFactory类是通过TryAddSingleton方法注册的,也就意味着其为单例,那么这个内部字典便是唯一的,每个键值对应的ActiveHandlerTrackingEntry对象也是唯一,该对象内部中包含着handler。

下一句代码StartHandlerEntryTimer(entry);
 开启了ActiveHandlerTrackingEntry对象的过期计时处理。默认过期时间是2分钟。
1 internal void ExpiryTimer_Tick(object state) 2 { 3 var active =
(ActiveHandlerTrackingEntry)state; 4 5 // The timer callback should be the
only one removing from the active collection. If we can't find 6 // our entry
in the collection, then this is a bug. 7 var removed =
_activeHandlers.TryRemove(active.Name,out var found); 8 Debug.Assert(removed, "
Entry not found. We should always be able to remove the entry"); 9
Debug.Assert(object.ReferenceEquals(active, found.Value), "Different entry
found. The entry should not have been replaced"); 10 11 // At this point the
handler is no longer 'active' and will not be handed out to any new clients.12
// However we haven't dropped our strong reference to the handler, so we can't
yet determine if13 // there are still any other outstanding references (we know
there is at least one).14 // 15 // We use a different state object to track
expired handlers. This allows any other thread that acquired16 // the 'active'
entry to use it without safety problems. 17 var expired = new
ExpiredHandlerTrackingEntry(active);18 _expiredHandlers.Enqueue(expired); 19 20
Log.HandlerExpired(_logger, active.Name, active.Lifetime);21 22
StartCleanupTimer();23 }
先是将ActiveHandlerTrackingEntry对象传入新的ExpiredHandlerTrackingEntry对象。
1 public ExpiredHandlerTrackingEntry(ActiveHandlerTrackingEntry other) 2 { 3
Name = other.Name; 4 5 _livenessTracker = new WeakReference(other.Handler); 6
InnerHandler = other.Handler.InnerHandler; 7 }
在其构造方法内部,handler对象通过弱引用方式关联着,不会影响其被GC释放。

然后新建的ExpiredHandlerTrackingEntry对象被放入专用的队列。

最后开始清理工作,定时器的时间间隔设定为每10秒一次。
1 internal void CleanupTimer_Tick(object state) 2 { 3 // Stop any pending
timers, we'll restart the timer if there's anything left to process after
cleanup. 4 // 5 // With the scheme we're using it's possible we could end up
with some redundant cleanup operations. 6 // This is expected and fine. 7 // 8
// An alternative would be to take a lock during the whole cleanup process.
This isn't ideal because it 9 // would result in threads executing
ExpiryTimer_Tick as they would need to block on cleanup to figure out10 //
whether we need to start the timer. 11 StopCleanupTimer(); 12 13 try 14 { 15
if (!Monitor.TryEnter(_cleanupActiveLock)) 16 { 17 // We don't want to run a
concurrent cleanup cycle. This can happen if the cleanup cycle takes18 // a
long time for some reason. Since we're running user code inside Dispose, it's
definitely19 // possible. 20 // 21 // If we end up in that position, just make
sure the timer gets started again. It should be cheap22 // to run a 'no-op'
cleanup. 23 StartCleanupTimer(); 24 return; 25 } 26 27 var initialCount =
_expiredHandlers.Count;28 Log.CleanupCycleStart(_logger, initialCount); 29 30
var stopwatch = ValueStopwatch.StartNew(); 31 32 var disposedCount = 0; 33 for (
var i = 0; i < initialCount; i++) 34 { 35 // Since we're the only one removing
from _expired, TryDequeue must always succeed. 36 _expiredHandlers.TryDequeue(
out var entry); 37 Debug.Assert(entry != null, "Entry was null, we should
always get an entry back from TryDequeue"); 38 39 if (entry.CanDispose) 40 { 41
try 42 { 43 entry.InnerHandler.Dispose(); 44 disposedCount++; 45 } 46 catch
(Exception ex)47 { 48 Log.CleanupItemFailed(_logger, entry.Name, ex); 49 } 50
}51 else 52 { 53 // If the entry is still live, put it back in the queue so
we can process it54 // during the next cleanup cycle. 55
_expiredHandlers.Enqueue(entry);56 } 57 } 58 59 Log.CleanupCycleEnd(_logger,
stopwatch.GetElapsedTime(), disposedCount, _expiredHandlers.Count);60 } 61
finally 62 { 63 Monitor.Exit(_cleanupActiveLock); 64 } 65 66 // We didn't
totally empty the cleanup queue, try again later. 67 if (_expiredHandlers.Count
>0) 68 { 69 StartCleanupTimer(); 70 } 71 }
上述方法核心是判断是否handler对象已经被GC,如果是的话,则释放其内部资源,即网络连接。

回到最初创建HttpClient的代码,会发现并没有传入任何name参数值。这是得益于HttpClientFactoryExtensions类的扩展方法。
1 public static HttpClient CreateClient(this IHttpClientFactory factory) 2 { 3
if (factory == null) 4 { 5 throw new ArgumentNullException(nameof(factory)); 6
}7 8 return factory.CreateClient(Options.DefaultName); 9 }
Options.DefaultName的值为string.Empty。


DefaultHttpClientFactory缺少无参数的构造方法,唯一的构造方法需要传入多个参数,这也意味着构建它时需要依赖其它一些类,所以目前只适用于在ASP.NET程序中使用,还无法应用到诸如控制台一类的程序,希望之后官方能够对其继续增强,使得应用范围变得更广。
1 public DefaultHttpClientFactory( 2 IServiceProvider services, 3
ILoggerFactory loggerFactory,4 IOptionsMonitor<HttpClientFactoryOptions>
optionsMonitor,5 IEnumerable<IHttpMessageHandlerBuilderFilter> filters)
 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信