【发布时间】:2009-11-30 02:44:10
【问题描述】:
如何获取导致 WCF 客户端发生故障事件的异常或错误代码?
private void ConnectionFaultedHandler(object sender, EventArgs e)
{
// Darn! Something went wrong.
// Better clean up.
this._Connection.Abort();
this._Connection.InnerChannel.Faulted -= this.ConnectionFaultedHandler;
// I'd really like to tell the user some detail of what blew up.
// But there is no Exception (or similar) property on the event.
this.NotifyUIOfConnectionFailure(e.Exception);
}
注意,这类似于this thread,除了我 1) 无法以这种方式工作,2) 它似乎正在解决服务端的问题,我想在客户端处理.
编辑:
为了澄清,上述处理程序是长时间(数小时甚至数天)保持打开状态的连接的一部分;它有一个回调接口来接收来自服务的数据。当您调用 Open 之类的方法或属于合约接口的方法时,我不是在询问异常情况,而是由于(例如)有人从您的 PC 上拔下网线或您的 Internet 连接失败而发生的故障。
想象一下它发生在这段代码执行后的某个时间:
private void OpenConnection()
{
try
{
this._Connection.Open();
}
catch (Exception ex)
{
// Yes, I should be catching CommunicationsException,
// and TimeoutException, but space is short on StackOverflow.
return;
}
Debug.Assert(this._Connection.State == Open);
this._Connection.InnerChannel.Faulted += this.ConnectionFaultedHandler;
}
【问题讨论】: