2012-06-01 41 views
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实例接收Faulted事件,即一次

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


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

+0

你可以发布'this.FaultedHandler'方法吗? –

+0

我用FaultedHandler代码更新了这个问题。 –

+0

你现在可以发布'this.CommunicationObject'属性吗?如果你可以发布全班,这可能会更容易。 –

回答

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(); 
} 
} 
相关问题