简体   繁体   English

使用Web服务代理在类中实现IDisposable

[英]Implement IDisposable in Class with a Web Service Proxy

The one area of .NET that's always befuddled me is exactly when to implement the IDisposable pattern. .NET一直困扰我的一个领域就是何时实现IDisposable模式。

I have created a SOAP web service proxy using WSDL which implements IDisposable on the Component base class: 我使用WSDL创建了一个SOAP Web服务代理,该代理在Component基类上实现了IDisposable:

public partial class WSDLGeneratedProxy : System.Web.Services.Protocols.SoapHttpClientProtocol

I have created sort-of facade interface with simplified methods so that I could hide the service proxy interactions behind, and made it implement IDisposable: 我用简化的方法创建了sort-of Facade接口,以便可以隐藏服务代理交互,并使其实现IDisposable:

public interface IServiceClient : IDisposable

I have an implementation of the IServiceClient and that implementation contains an actual member of WSDLGeneratedProxy 我有一个IServiceClient的实现,并且该实现包含WSDLGeneratedProxy的实际成员

public class FacadeServiceClient : IServiceClient
{
    private WSDLGeneratedProxy clientProxy;
}

So my question is - should I call the Dispose method on the service proxy explicitly? 所以我的问题是-我应该在服务代理上显式调用Dispose方法吗? Is that the proper way to do it? 那是正确的方法吗?

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

protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        if (clientProxy != null)
        {
            clientProxy.Dispose();
        }
    }
}

Any suggestions or input is greatly appreciated. 任何建议或意见,不胜感激。

I don't see any reason for which you would have to dispose that object, it's going to be garbage collected anyways when it's no longer used. 我看不到有什么理由需要处置该对象,无论何时不再使用它,都将被垃圾回收。

IDisposable interface base use is to dispose unmanaged objects (check Proper use of the IDisposable interface ) IDisposable接口底座的用途是处置非托管对象(检查正确使用IDisposable接口的

That's the reason why we use using with for example System.IO classes like FileStream 这就是为什么我们使用的原因using与例如System.IO类像FileStream

      using (var fs = new FileStream(filePath, mode)) 
       {
              //use fs 
       }

When the compiler encounters using keyword it will rewrite the code into something like 当编译器遇到using关键字时,它将代码重写为类似

        FileStream fs = new FileStream(filePath, mode); 
        try 
        {
            //use fs
         }
        finally 
        {
            //compiler will automatically call dispose on the FileStream to free up unmanaged objects
             fs.Dispose(); 
         }

You may also want to use dispose when you work with huge objects in memory (like hundreds of MB) and do not wish to wait for the GC to collect them but rather do it earlier. 当您处理内存中的巨大对象(例如数百MB)时,您可能还希望使用Dispose,而不希望等待GC收集它们,而是更早地进行处理。

If you're not in any of the mentioned above situations then the GC will for sure do better work in disposing the objects than you will do. 如果你不能在任何上述情况,则GC将肯定做的更好的工作在处置比你做的对象提到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM