2012-01-25 31 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