tags:

views:

122

answers:

2

Before I call Close() on my WCF service, should I check to see if it is not already closed?

ie.

myWCFService.State != System.ServiceModel.CommunicationState.Closed

My code looks like:

MyServiceClient myWCFClient = null;

try { myWCFClient = new .....(); } catch { } finally { myWCFClient.Close(); }

+2  A: 

A WCF client is disposable, so except for a few caveats you can use using:

using(MyClient client = new MyClient()) {
    client.DoStuff();
    // etc
}

But there is a big problem with this; the Dispose on the WCF client actually throws if it is faulted (losing the original exception). There is a good workaround, here, or I've blogged on this here.

Marc Gravell
+1  A: 

Take a look at this question: What is the best workaround for the WCF client using block issue? Although it isn't word for word what you are looking for, I think his examples will help you out.

siz