在生產環境中,發現了IIS連接池假死問題,排查了很久,最終定位到 用到 ChannelFactory 的通信管道
原始代碼使用Using 來關閉,因爲請求的服務器一直正常響應,未發現問題,實際上,如果服務器掛了,Using是無法關閉ChannelFactory的
Close()方法仍舊會繼續拋出多種異常,並且需要多個環節處理關閉通信管道,否者,將導致應用程序池假死。
貼下代碼:
using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(new NetTcpBinding(SecurityMode.None), T.t))
{
T proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
try
{
proxy.method(entity);
}
catch(TimeoutException){
(proxy as ICommunicationObject).Abort();
}
catch (CommunicationException)
{
(proxy as ICommunicationObject).Abort();
}
catch (Exception ex)
{
}
}