简体   繁体   English

配置Web服务代理类?

[英]Dispose a Web Service Proxy class?

When you create and use a Web Service proxy class in the ASP.Net framework, the class ultimately inherits from Component, which implements IDisposable. 当您在ASP.Net框架中创建和使用Web服务代理类时,该类最终继承自实现IDisposable的Component。

I have never seen one example online where people dispose of a web proxy class, but was wondering if it really needs to be done. 我从未在网上看到一个人们处理Web代理类的例子,但是想知道它是否真的需要完成。 When I call only one method, I normally wrap it in a using statement, but if I have a need to call it several times throughout the page, I might end up using the same instance, and just wondered what the ramifications are of not disposing it. 当我只调用一个方法时,我通常将它包装在using语句中,但如果我需要在整个页面中多次调用它,我可能最终会使用相同的实例,并且只是想知道没有处理的后果是什么它。

You need to dispose everything that implements IDisposable . 您需要处理实现IDisposable 所有内容

The implementation of IDisposable signifies that the object holds onto something that the garbage collector doesn't intrinsically track - might be a connection, might be a file, might be some other handle - or might be nothing at all, it doesn't really matter. 实施IDisposable表示该对象保存到的东西 ,垃圾收集器本质上并不跟踪-可能是一个连接,可能是一个文件,可能是一些其他的手柄-或者可能是什么都没有,它并不真正的问题。 You shouldn't concern yourself with the implementation details because those are subject to change. 您不应该关注实现细节,因为这些细节可能会发生变化。

The class may or may not truly use unmanaged resources. 该类可能会也可能不会真正使用非托管资源。 It may or may not have a finalizer. 它可能有也可能没有终结器。 It makes little difference; 它没什么区别; if the class implements IDisposable , then it's asking you to Dispose it when you're done. 如果该类实现了IDisposable ,则它会要求您在完成后将其Dispose Even if the Dispose method does nothing, you never know when somebody will replace the reference to that class with a reference to a subclass that really does need to be disposed. 即使Dispose方法不做任何事情,你永远不知道什么时候有人会用一个对确实需要被置于一个子类的引用替换引用这个类。

The short answer is that with Web Service Proxy Classes, you should Close them and not Dispose them. 简短的回答是,对于Web服务代理类,您应该关闭它们而不是处理它们。

In almost every case you should dispose of things that implement IDisposable. 在几乎每种情况下,您都应该处理实现IDisposable的东西。 However, Web Service Proxy classes are a special case. 但是,Web服务代理类是一种特殊情况。 With these classes, and all classes that inherit from System.ServiceModel.ClientBase , it is a best practice to not call dispose but to call the Close method directly. 使用这些类以及从System.ServiceModel.ClientBase继承的所有类,最好不要调用dispose,而是直接调用Close方法。

Using reflector, you can see that the Dispose method of ClientBase simply calls Close . 使用反射器,您可以看到ClientBaseDispose方法只是调用Close So if there are no exceptions, Dispose and Close will do the same thing. 因此,如果没有例外, DisposeClose将执行相同的操作。 However if there is an exception, there will be different behaviors. 但是,如果存在异常,则会有不同的行为。

Because the Close method can throw exceptions, you should call it directly and catch it's exception. 因为Close方法可以抛出异常,所以应该直接调用它并捕获它的异常。 If you call the Dispose method, you should also catch the exceptions, but your code will be harder to understand. 如果您调用Dispose方法,您还应该捕获异常,但您的代码将更难理解。

This also means that you should avoid putting the declaration of the proxy in a using statement. 这也意味着您应该避免将代理声明放在using语句中。 In this case, if an exception is thrown in the using block, it will be obscured. 在这种情况下,如果在using块中抛出异常,它将被遮挡。 The Dispose call that is auto generated by the using block will get called because it is in a finally block. usingusing块自动生成的Dispose调用,因为它位于finally块中。 The exception that is thrown from the Close in the Dispose will obscure whatever exception was previously thrown. DisposeClose抛出的异常将模糊先前抛出的异常。

To see more detailed explinations, read these articles on MSDN , Coding Up Style , BlogginAbout.Net , and StackOverflow . 要查看更详细的说明,请阅读MSDNCoding Up StyleBlogginAbout.NetStackOverflow上的这些文章。

For the backstory on why it is implemented this way, check on this thread on the MSDN forums . 有关为何以这种方式实现它的背景故事,请查看MSDN论坛上的此主题。

Sometimes Dispose() is just a dummy (inherited from a base class), but then it is still a good practice to call it, to be safe for future changes. 有时Dispose()只是一个虚拟(从基类继承),但是调用它仍然是一个很好的做法,对于将来的更改是安全的。

A WebService/WCF proxy is holding a connection, so it is certainly a good idea to call Dispose() or Close(). WebService / WCF代理正在保持连接,因此调用Dispose()或Close()当然是个好主意。 Doing so inside a using block is of course preferable since it is exception safe. 在使用块内部这样做当然是优选的,因为它是异常安全的。

But in your case (using the proxy in multiple methods on your page) the use of multiple using blocks probably would take a performance hit. 但在您的情况下(在您的页面上使用多个方法中的代理),使用多个使用块可能会受到性能影响。 It is OK to replace the using blocks with a call Close() in an event that comes late in the page cycle. 可以在页面循环后期的事件中使用调用Close()替换使用块。 I'm not sure about ASP.NET best practices here, but I would use OnPreRender or OnPageUnload or something. 我不确定这里的ASP.NET最佳实践,但我会使用OnPreRender或OnPageUnload等。

You will loose exception safety here but that is not a fundamental problem, the GC will solve that. 你会在这里放松异常安全,但这不是一个根本问题,GC会解决这个问题。

IMHO, there's nothing like disposing a web-service proxy class. 恕我直言,没有什么比处理Web服务代理类更好的了。

Most of the proxies are: 大多数代理是:

  • Stateless, as such calling using one instance or using multiple instances does not make any difference 无状态,因此使用一个实例或使用多个实例进行调用没有任何区别
  • Intermittent meaning that they close the connection as soon as all response is processed 间歇性意味着一旦处理完所有响应,它们就会关闭连接

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

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