开发者

WCF client goes to faulted immediately

开发者 https://www.devze.com 2023-01-09 11:21 出处:网络
I have the following code for recreating a WCf client in a non opened state if (client.State != CommunicationState.Opened)

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>("NetTcpBindingEnd开发者_JS百科point", 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消