双工信道断陷事件不会上升,对第二次连接尝试(Duplex channel Faulted event

2019-07-29 17:00发布

我有定期的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被指责的事件吗? 是否有任何变通办法?


类似的非回答的问题: 没有运输安全WCF可靠的会话不会发生故障事件的时间

Answer 1:

后对WCF战争两天我已经找到了解决办法。

有时WCF触发Faulted事件,但有时没有。 然而, Closed事件始终点火,尤其是后Abort()调用。

所以,我呼吁Abort()FaultedHandler有效地触发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();
}
}


文章来源: Duplex channel Faulted event does not rise on second connection attempt