【问题标题】:Duplex channel Faulted event does not rise on second connection attempt双工通道故障事件不会在第二次连接尝试时出现
【发布时间】:2012-06-06 13:16:21
【问题描述】:

我有常规的 net.tcp WCF 服务客户端和常规的 net.tcp duplex(即带有回调)WCF 服务客户端。我已经实现了一些逻辑来不断地重新实例化连接,以防服务出现故障。

它们的创建方式完全相同:

FooServiceClient Create()
{
    var client = new FooServiceClient(ChannelBinding);
    client.Faulted += this.FaultedHandler;
    client.Ping(); // An empty service function to make sure connection is OK
    return client;
}

BarServiceClient Create()
{
    var duplexClient = new BarServiceClient(new InstanceContext(this.barServiceCallback));
    duplexClient.Faulted += this.FaultedHandler;
    duplexClient.Ping(); // An empty service function to make sure connection is OK
    return duplexClient;
}

public class Watcher
{
public Watcher()
{
    this.CommunicationObject = this.Create();
}

ICommunicationObject CommunicationObject { get; private set; }

void FaultedHandler(object sender, EventArgs ea)
{
    this.CommunicationObject.Abort();
    this.CommunicationObject.Faulted -= this.FaultedHandler;
    this.CommunicationObject = this.Create();
}
}

FaultedHandler() 中止通道并使用上面的代码重新创建它。

FooServiceClient 重新连接逻辑工作正常,在多次故障后重新连接。然而,几乎相同但双工的BarServiceClient 仅从第一个BarServiceClient 实例接收故障事件,即一次

为什么只有双工BarServiceClient 的第一个实例会发生故障事件?有什么解决方法吗?


一个类似的未回答问题:WCF Reliable session without transport security will not faulted event on time

【问题讨论】:

  • 你能发布this.FaultedHandler方法吗?
  • 我已经用 FaultedHandler 代码更新了问题。
  • 您现在可以发布this.CommunicationObject 属性吗?如果您可以发布整个课程,可能会更容易。
  • 实际上代码被分散到三个类中。其中两个是工厂(创建方法)。第三个是重新创建连接的“观察者”类。

标签: c# .net wcf wcf-4 duplex-channel


【解决方案1】:

经过两天与 WCF 的战争,我找到了解决方法。

有时 WCF 会触发 Faulted 事件,但有时不会。然而,Closed 事件总是被触发,尤其是在Abort() 调用之后。

所以我在FaultedHandler 中调用Abort(),这实际上触发了Closed 事件。随后,ClosedHandler 执行重新连接。如果框架从不触发Faulted,则始终触发Closed 事件。

BarServiceClient Create()
{
    var duplexClient = new BarServiceClient(new InstanceContext(this.barServiceCallback));
    duplexClient.Faulted += this.FaultedHandler;
    duplexClient.Closed += this.ClosedHandler;
    duplexClient.Ping(); // An empty service function to make sure connection is OK
    return duplexClient;
}

public class Watcher
{
public Watcher()
{
    this.CommunicationObject = this.Create();
}

ICommunicationObject CommunicationObject { get; private set; }

void FaultedHandler(object sender, EventArgs ea)
{
    this.CommunicationObject.Abort();
}

void ClosedHandler(object sender, EventArgs ea)
{
    this.CommunicationObject.Faulted -= this.FaultedHandler;
    this.CommunicationObject.Closed -= this.ClosedHandler;
    this.CommunicationObject = this.Create();
}
}

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2012-11-20
    • 2010-09-17
    • 2010-12-06
    • 1970-01-01
    相关资源
    最近更新 更多