Do I need to close a .NET service reference client when I'm done using it

后端 未结 3 1502
耶瑟儿~
耶瑟儿~ 2021-02-01 21:05

I\'m trying to find out if it is neccessary to close a .net service reference client when you are done using it. Almost all of the examples that I have come across on the net d

3条回答
  •  没有蜡笔的小新
    2021-02-01 21:50

    The best thing to do is look at the generated client code for Dispose() and see if it's really disposing of anything, like HTTP connections or something.

    On one hand, it may just be that the interface it implements inherits from IDisposable because some client might need to dispose of something, even though that particular one doesn't. That's similar to MemoryStream, a class that implements IDisposable because all Streams do, but that doesn't actually deal with any unmanaged resources.

    On the other hand, it never hurts to use using, even if Dispose() is an empty method. And MS examples are actually really bad about not using using even if they should (e.g. here), so don't take their example as good evidence that you don't need to.

提交回复
热议问题