3

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

i.e.

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

My code looks like:

MyServiceClient myWCFClient = null;

try
{
  myWCFClient = new .....();
}
catch
{
}
finally
{
   myWCFClient.Close();
}
Jason Plank
  • 2,342
  • 4
  • 32
  • 40
Blankman
  • 248,432
  • 309
  • 736
  • 1,161

2 Answers2

2

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
  • 976,458
  • 251
  • 2,474
  • 2,830
1

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.

Community
  • 1
  • 1
Szymon Rozga
  • 17,560
  • 5
  • 51
  • 65