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,336
  • 5
  • 31
  • 40
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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,971
  • 7
  • 53
  • 66