简体   繁体   中英

Do I need to dispose a web service reference in ASP.NET?

垃圾收集器是否清理了Web服务引用,或者在调完我调用的任何方法后,是否需要在服务引用上调用dispose?

Instead of worrying about disposing your web services, you could keep only a single instance of each web service, using a singleton pattern . Web services are stateless, so they can safely be shared between connections and threads on a web server.

Here is an example of a Web Service class you can use to hold references to your web service instances. This singleton is lazy and thread-safe. It is advised that if you make your singletons lazy, they are also kept thread safe by following the same logic. To learn more about how to do this, read the C# In Depth article on Implementing Singletons .

Also keep in mind that you may run into issues with WCF web services. I'd recommend reading up on WCF's instance management techniques article , specifically the singleton section, for more details.

public static class WS
{
    private static object sync = new object();
    private static MyWebService _MyWebServiceInstance;

    public static MyWebService MyWebServiceInstance
    {
        get
        {
            if (_MyWebServiceInstance == null) 
            {
              lock (sync)
              {
                if (_MyWebServiceInstance == null)
                {
                    _MyWebServiceInstance= new MyWebService();
                }
              }
            }
            return _MyWebServiceInstance;
        }
    }
}

And then when you need to access your web service, you can do this:

WS.MyWebServiceInstance.MyMethod(...)

or

var ws = WS.MyWebServiceInstance;
ws.MyMethod(...)

I've successfully used this pattern on several projects and it has worked well, but as tvanfosson mentions in the comments below, an even better strategy would be to use a DI framework to manage your web service instances.

我认为DataService从Component继承Dispose。

Objects that implement IDispose should be disposed of manually to assist the garbage collector.

If you object is short lived use a using block. For objects that can be retained ensure that they object that retains them disposes of them when it is also disposed.

what are you trying to accomplish here?

If your worried about performance, then I would worry more about the responsiveness of the server hosting the webservice and the network speed, as they can dramatically affect the length of time you have to wait for the webservice call to complete (unless its asynchronous).

The examples on MSDN dont call 'Dispose' and its quite obvious that the garbage collector will do its job, so unless your working on a realtime system that needs to process over 100,000 records in memory every second, then maybe you dont need to come up with a way to dispose resources or manage memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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