【问题标题】:Disposing client after creation with WebChannelFactory使用 WebChannelFactory 创建后处理客户端
【发布时间】: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


    【解决方案1】:

    这是一个非常复杂的问题。即使Microsoft's 自己承认,处理渠道工厂也是一个糟糕的设计,被多次更改,所以简短的回答是否定的,你需要使用其他替代方法。

    这是一个alternative 的处理方法。

    【讨论】:

    • 您并没有真正回答我的问题,但您为我提供了很好的阅读,我接受您的回答。谢谢。
    猜你喜欢
    • 2014-01-16
    • 2017-10-23
    • 2015-01-02
    • 2018-10-02
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多