双工通道故障事件在第二次连接尝试时不会上升

我有常规的net.tcp WCF服务客户端,以及常规的net.tcp 双工 (即带回调)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事件,即一次

为什么只有双工BarServiceClient的第一个实例出现故障事件? 有没有解决方法?


一个类似的未回答的问题: WCF没有传输安全性的可靠会话不会导致事件按时发生故障

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