【发布时间】:2011-07-01 13:37:21
【问题描述】:
这就是我理解为什么我们需要为 serviceClients 实现自己的包装类的原因(如果我错了,请纠正我):
捕获异常的目的 在 Dispose() 方法中是 .Close() 可能会抛出一个 “通讯异常”或 “TimeoutException” - 并防止 挂你的连接抓住那些 使用 .Abort() 的两个例外 将改变状态 通讯对象关闭 立即。 - 它不会使任何 让例外成为意义 未处理,因为必要的电话 方法已经制定 因为我们在 Dispose() 部分,并且 因此,抛出一个很奇怪 当工作实际上是异常时 应该做的。
但为什么要这样做:
public class ServiceClientWrapper<TServiceType> : IDisposable
{
public TServiceType Channel { get; private set; }
private readonly ChannelFactory<TServiceType> _channelFactory;
public ServiceClientWrapper(string endpoint)
{
_channelFactory = new ChannelFactory<TServiceType>(endpoint);
Channel = _channelFactory.CreateChannel();
((IChannel)Channel).Open();
}
#region Implementation of IDisposable
public void Dispose()
{
try
{
((IChannel)Channel).Close();
}
catch (CommunicationException ex)
{
((IChannel)Channel).Abort();
}
catch (TimeoutException ex)
{
((IChannel)Channel).Abort();
}
catch (Exception)
{
((IChannel)Channel).Abort();
throw;
}
}
#endregion
}
什么时候可以做:
public class ServiceClientWrapper<TServiceType> : IDisposable
{
public TServiceType Channel { get; private set; }
private readonly ChannelFactory<TServiceType> _channelFactory;
public ServiceClientWrapper(string endpoint)
{
_channelFactory = new ChannelFactory<TServiceType>(endpoint);
Channel = _channelFactory.CreateChannel();
((IChannel)Channel).Open();
}
#region Implementation of IDisposable
public void Dispose()
{
((IChannel)Channel).Abort();
}
#endregion
}
根据 MSDN,.Close() 和 .Abort() 都会将通信对象的状态更改为“关闭”?
【问题讨论】:
标签: wcf