在使用后立即在 WCF ChannelFactory<T> 和 T 通道对象上调用 dispose 有多重要?

How important is to call dispose on a WCF ChannelFactory<T> and T channel object right after usage?

开发人员喜欢编写尽可能简短的代码。我们在 C# 中调用 WCF 服务,想知道以下直觉是对还是错。 使用 ChannelFactory 代码的一种方法是:

var factory = new ChannelFactory<IService>("Endpoint");
var client = factory.CreateChannel();

client.DoSomething();

((IClientChannel)client).Close();
factory.Close();

其他方式(更简洁)是使用包装器对象(用于工厂和通道),它实现了 IDisposable 其中处理两者并用于 using 块:

using (ServiceWrapper<IService> svcWrapper = new ServiceWrapper<IService>("Endpoint")) {
    svcWrapper.Channel.DoSomething();
}

在这里,必须调用包装器的 属性 对于开发人员来说可能有点烦人。基本上也可以是:

using (ServiceWrapper<IService> svcWrapper = new ServiceWrapper<IService>("Endpoint")) {
    IService client = svcWrapper.Channel;
    client.DoSomething();
}

(我还发现this MSDN article说using块可以隐藏异常)

开发人员可能更喜欢的是:

 IService client = new ServiceWrapper<IService>("Endpoint").Channel;
 client.DoSomething();

摘自《C# 5.0 简述》一书:

A popular pattern is to have the finalizer call Dispose. This makes sense when cleanup is non-urgent and hastening it by calling Dispose is more of an optimization than a necessity.

如果我将此添加到我的 ServiceWrapper(C# Finalize/Dispose 模式):

public void Dispose() {
    Dispose(true);
    GC.SuppressFinalize(this);
}

~ServiceWrapper() {
    Dispose(false);
}

然后GC最终会调用Dispose,做该做的事情。即:

public void Dispose(bool disposing) {
    if (disposing) {
        if (_channel != null) {
            ((IClientChannel)_channel).Close();
        }
        if (_factory != null) {
            _factory.Close();
        }

        _channel = null;
        _factory = null;
    }
}

我觉得这不是与 WCF ChannelFactory<T>T 通道一起使用的好习惯,但我没有一个好的解释会吓跑开发人员使用它。你们能解释一下吗?

我不能说它不起作用,我试过了,效果很好。我看不到任何明显的性能差异或类似... 除了 "You don't know when the finalizer gets called." 我无法给出其他解释 谢谢!

好吧,Dispose 方法所做的唯一一件事就是显式关闭连接。就这些。您可以在 reference source. Also, ClientBase<T> 中亲眼看到,连接的基础 class 也是如此。

就是说,如果您自己调用 Close 目前根本不需要调用 Dispose,尽管我认为您确实应该 Dispose ,只是为了证明未来。此外,这对于发生可能阻止 Close 被调用的错误也是一种故障保护。