Closing and Disposing a WCF Service
-
09-06-2019 - |
Question
The Close
method on an ICommunicationObject
can throw two types of exceptions as MSDN outlines here. I understand why the Close
method can throw those exceptions, but what I don't understand is why the Dispose
method on a service proxy calls the Close
method without a try
around it. Isn't your Dispose
method the one place where you want make sure you don't throw any exceptions?
Solution
It seems to be a common design pattern in .NET code. Here is a citation from Framework design guidelines
Consider providing method Close(), in addition to the Dispose(), if close is standard terminology in the area. When doing so, it is important that you make the Close implementation identical to Dispose ...
Here is a blog post in which you can find workaround for this System.ServiceModel.ClientBase design problem
OTHER TIPS
Yes, typically Dispose is one of the places you want to ensure exceptions aren't thrown. However, based on this MSDN forum thread there were some historical reasons for this behavior. As such, the recommended pattern is the try{Close}/catch{Abort} paradigm.