chu*_*h97 11 c# wcf idisposable wcf-4
我使用以下方法关闭WCF 4通道.这是正确的方法吗?
using (IService channel
= CustomChannelFactory<IService>.CreateConfigurationChannel())
{
channel.Open();
//do stuff
}// channels disposes off??
Run Code Online (Sandbox Code Playgroud)
Enr*_*lio 20
这曾经是在WCF"早期"发布WCF客户端代理的普遍接受的方式.
然而事情发生了变化.事实证明,IClientChannel <T> .Dispose()的实现只是调用IClientChannel <T> .Close()方法,这可能会在某些情况下抛出异常,例如底层通道未打开或可以'及时关闭.
因此,Close()在catch块内调用并不是一个好主意,因为如果出现异常,可能会留下一些未发布的资源.
的新推荐的方法是调用IClientChannel <T> .Abort()的内catch块代替,在情况下Close()会失败.这是一个例子:
try
{
channel.DoSomething();
channel.Close();
}
catch
{
channel.Abort();
throw;
}
Run Code Online (Sandbox Code Playgroud)
更新:
以下是对描述此建议的MSDN文章的引用.
虽然不是严格针对频道,但您可以这样做:
ChannelFactory<IMyService> channelFactory = null;
try
{
channelFactory =
new ChannelFactory<IMyService>();
channelFactory.Open();
// Do work...
channelFactory.Close();
}
catch (CommunicationException)
{
if (channelFactory != null)
{
channelFactory.Abort();
}
}
catch (TimeoutException)
{
if (channelFactory != null)
{
channelFactory.Abort();
}
}
catch (Exception)
{
if (channelFactory != null)
{
channelFactory.Abort();
}
throw;
}
Run Code Online (Sandbox Code Playgroud)