Wcf client WCF客户端立即进入故障状态

Wcf client WCF客户端立即进入故障状态,wcf-client,Wcf Client,我有以下代码用于在非打开状态下重新创建WCf客户端 if (client.State != CommunicationState.Opened) { client.Abort(); client = null; Trace.WriteLine("Client object in non opened state. Recreating object"); client = new <WCFClient>("NetTcpBindingEndpoint",

我有以下代码用于在非打开状态下重新创建WCf客户端

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();
}
然而,由于某种原因,当这个例程返回并且我尝试调用client.Somemethod时,我会得到一个异常,当我捕获它时,我会看到客户端处于故障状态。我不明白这怎么会发生得这么快

提前谢谢你的帮助


Subbu

当您尝试呼叫client.SomeMethod时,您能告诉我们吗

我看不出你想通过客户实现什么。在这里打开。。。。。这真的没有任何意义-只需调用您想要调用的方法

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();
}

也许再加上一些尝试……抓住它周围的魔力,让它更安全。。。。但这确实是您所需要的全部-无需先打开客户端…

我有一个windows服务,它通过WCF将状态消息发送回WinForm应用程序。这些信息发送速度很快。因此,我没有打开、使用方法并从windows服务关闭WCF客户端,而是尝试保持它的打开状态,并在代码的其他部分调用它的方法。但是,在调用客户端的方法时,我在windows服务代码中编写了另一个名为CheckClientStatus的例程,该例程检查客户端的状态,并在对象处于未打开状态时重新创建该对象。