2009-11-30 44 views
2

如何获得导致WCF客户端的故障事件的异常或错误代码?我如何得到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; 
} 

回答

0

你的问题不是那么清楚,通过接收故障是由接收异常不同的方式

无论何时抛出一个异常对象,该异常对象将在具有相应的catch块的客户端上接收。

在通道出现问题时收到故障(未连接)...