2009-11-30 47 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塊的客戶端上接收。

在通道出現問題時收到故障(未連接)...