tags:

views:

30

answers:

1

Hi,

I have the following code for recreating a WCf client in a non opened state

if (client.State != CommunicationState.Opened)
{
   client.Abort();

   client = null;

   Trace.WriteLine("Client object in non opened state. Recreating object");

   client = new <WCFClient>("NetTcpBindingEndpoint", ConfigurationManager.AppSettings["ServiceEndPointAddress"]);

   client.Open();
}

For some reason though, as soon as this routine returns and I try to call client.Somemethod(), I get an exception and when I catch it, I see the client in a faulted state. I don't understand how this happened so quickly.

Thanks for any help in advance.

Subbu

A: 

Can you show us when you're trying to call client.SomeMethod() ?

I don't see what you're trying to achieve with the client.Open() here..... that really doesn't make any sense at all - just call the method you want to call!

try
{          
    var client = new <WCFClient>("NetTcpBindingEndpoint", ConfigurationManager.AppSettings["ServiceEndPointAddress"]);

    client.SomeMethod();
    client.Close();
}
catch(FaultException<T> exc)
{
   // handle it
    client.Abort();
}
catch(CommunicationException exc)
{
   // handle it
    client.Abort();
}
catch(EndpointNotFoundException exc)
{
   // handle it
    client.Abort();
}
catch(TimeoutException exc)
{
   // handle it
    client.Abort();
}

and maybe add some try.....catch magic around it to make it safer.... but that's really all you need - no need to first .Open() the client.....

marc_s
I have a windows service which sends back status messages through WCF to a WinForm application. These messages get sent in a fast pace. So, instead of opening, using a method and closing the WCF client from the windows service, I am trying to keep it open and call its methods in other parts of the code. But while calling the client's methods, I have coded another routine called CheckClientStatus() in the windows service code which checks the state of the client and recreates the object if it is in a non opened state.
Subbu

related questions