7

我正在尝试为 wcf 客户端实现重新连接逻辑。我知道您必须在当前频道进入故障状态后创建一个新频道。我在通道故障事件处理程序中执行了此操作:


internal class ServiceClient : DuplexClientBase, IServiceClient
{
  public ServiceClient(ICallback callback, EndpointAddress serviceAddress)
   : base(callback, MyUtility.GetServiceBinding("NetTcpBinding"), serviceAddress)
  {
   // Open the connection.
   Open();
  }

  public void Register(string clientName)
  {
    // register to service
  }

  public void DoSomething()
  {
    // some code
  }
 }

 public class ClientApp
 {
  private IServiceClient mServiceClient;

  private ICallback mCallback;

  public ClientApp()
  {
   mServiceClient = new ServiceClient( mCallback, new EndpointAddress("someAddress"));

   mServiceClient.Register();

   // register faulted event for the  service client
   ((ICommunicationObject)mServiceClient).Faulted += new EventHandler(ServiceClient_Faulted);
  }

  void ServiceClient_Faulted(object sender, EventArgs e)
  {
   // Create new Service Client.
   mServiceClient = new ServiceClient( mCallback, new EndpointAddress("someAddress"));

   // Register the EI at Cell Controller
   mServiceClient.Register();
  }

  public void DoSomething()
  {
   mServiceClient.DoSomething();
  }
 }

但是在我的单元测试中,我仍然得到一个“通信对象 System.ServiceModel.Channels.ServiceChannel,因为它处于故障状态,所以不能用于通信”异常。

回调通道是否仍有可能出现故障,如果是,我该如何更换回调通道?

4

2 回答 2

5

到目前为止,我已经经历过需要在故障时重新创建 WCF 连接 - 否则似乎没有办法恢复它。至于何时发生故障,该方法似乎可以正常触发,但通常它会在当前请求进行时触发并清理 WCF 连接(建立新连接等) - 导致此失败 - 特别是在超时时。

一些建议: - 如果与超时有关,请跟踪上次调用的时间和包含超时值的常量。如果 WCF 连接由于不活动而被丢弃,请在通过网络发送请求之前将其丢弃并重新创建它。- 另一件事,看起来你没有重新添加故障处理程序,这意味着第一个故障将得到处理,但第二次故障它将在没有处理程序的情况下翻倒,因为没有附加新的处理程序。

希望这可以帮助

于 2009-10-30T10:42:19.033 回答
0

您是否尝试通过在 Faulted 事件处理程序中调用 mServiceClient.Abort 来重置通信通道?

编辑:

我看到您没有在恢复代码中重新初始化 mCallback 对象。您可能需要将其分配给新实例。

于 2009-10-13T09:05:53.647 回答