Duplex channel Faulted event does not rise on second connection attempt

左心房为你撑大大i 提交于 2019-12-04 17:09:29
Vasyl Boroviak

After two days on the war against WCF I have found a workaround.

Sometimes WCF fires Faulted event, but sometimes it do not. However, the Closed event is always fired, especially after the Abort() call.

So I call Abort() in FaultedHandler which effectively fires Closed event. Subsequently, the ClosedHandler performs the reconnection. In case when Faulted is never fired by framework, the Closed event is always fired.

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();
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!