2012-01-25 29 views
2

我想用AppDomain.CurrentDomain.UnhandledException記錄我的異常。如何處理wcf服務回調異常

AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException; 
public static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
     HandleException(e.ExceptionObject as Exception); 
} 

當我在服務器例外,我將讓你在客戶端故障的調試時間,但鉤住AppDomain.CurrentDomain.UnhandledException事件將永遠不會被解僱。

服務器:

[ServiceBehavior(IncludeExceptionDetailInFaults=true)] 
public class Service : IService 
{   

    public string GetError() 
    { 
     throw new ApplicationException(); 
    } 
} 

客戶:

public void GetError() 
{ 
    this.service.BeginGetError(OnGetErrorCompleted, null); 
} 

public void OnGetErrorCompleted(IAsyncResult result) 
{ 
    var value = this.service.EndGetError(result); 
} 

這並不工作,除非我用Distpacher.BeginInvoke ..

public void OnGetErrorCompleted(IAsyncResult result) 
{ 
    this.Dispatcher.BeginInvoke((Action)(() => 
    { 
     var value = this.service.EndGetError(result); 
    }));   
} 

有人知道這是爲什麼? 我認爲AppDomain.CurrentDomain.UnhandledException將在任何線程發生異常時觸發! :S

回答

0

更好的解決方案是使用類似IErrorHandler的東西並拋出FaultExceptions。裝飾您的操作以使用FaultContract屬性引發FaultExceptions。現在您可以在客戶端捕獲特定的FaultExceptions。您不應將服務器上的異常暴露給客戶端。

+0

我已經[ServiceBehavior(IncludeExceptionDetailInFaults = true)]將異常轉換爲服務器中的故障。我也嘗試使用Entreprise庫異常來進行故障轉換,並像您說的那樣使用FaultContracts。所以我沒有在客戶端收到例外,但是有錯誤。 –

+0

您仍然將您的服務器異常暴露給您的客戶端。你的客戶應該如何處理服務器上發生的事情?不要使用'IncludeExceptionDetailInFaults',而是使用類似'IErrorHandler'的東西來提供錯誤。 – diggingforfire