Search code examples
wcf-client

WCF client goes to faulted immediately


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


Solution

  • 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.....