【发布时间】:2011-08-10 11:01:38
【问题描述】:
在我目前的生产代码中,根据documentation on msdn,创建客户端的方式是这样的
using (WebChannelFactory<IServiceInterface> cf
= new WebChannelFactory<IServiceInterface>("http://service.url"))
{
IServiceInterface client = cf.CreateChannel();
client.CallTheMethod();
}
鉴于我有这个界面:
public interface IServiceInterface
{
void CallTheMethod();
}
但是我注意到由 WebChannelFactory 创建的对象客户端也实现了 IDisposable。所以我也想处理这个对象。除了:
之外,我没有找到其他方法using (WebChannelFactory<IServiceInterface> cf
= new WebChannelFactory<IServiceInterface>("http://service.url"))
using(IDisposable client = (IDisposable)cf.CreateChannel())
{
((IServiceInterface)client).CallTheMethod();
}
我觉得这很难看。所以:
- 我真的需要处理它吗?我的意思是,它可能是在您处置工厂时被处置的(如果工厂保留对它创建的每个对象的引用)?
- 如果有,您有更好的方法吗?
【问题讨论】:
标签: c# wcf dispose webchannelfactory