2

我有常规的 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 Reliable session without transport security will not faulted event on time

4

1 回答 1

1

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

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

所以我调用which 有效地触发Abort()事件。随后,执行重新连接。如果框架从未触发 when ,则始终触发该事件。FaultedHandlerClosedClosedHandlerFaultedClosed

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();
}
}
于 2012-06-01T14:48:30.213 回答