简体   繁体   English

如何处理WCF服务回调中的异常

[英]How to handle exceptions in wcf service call back

I am trying to log my exceptions with AppDomain.CurrentDomain.UnhandledException. 我正在尝试使用AppDomain.CurrentDomain.UnhandledException记录我的异常。

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

When I get an exception in the server I will get the fault in the client at debug time but the event hooked to AppDomain.CurrentDomain.UnhandledException will never be fired. 当我在服务器中收到异常时,我将在调试时在客户端中得到错误,但挂接到AppDomain.CurrentDomain.UnhandledException的事件将永远不会触发。

Server: 服务器:

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

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

Client: 客户:

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

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

This doesn't work UNLESS I use Distpacher.BeginInvoke.. 除非我使用Distpacher.BeginInvoke,否则这将无法工作。

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

Somebody knows why?? 有人知道为什么吗? I thought AppDomain.CurrentDomain.UnhandledException would be fired in case of an exception in ANY thread! 我认为AppANY.CurrentDomain.UnhandledException将在任何线程中发生异常的情况下被触发! :S :S

A better solution is to use something like IErrorHandler and throw FaultExceptions. 更好的解决方案是使用IErrorHandler之类的东西并抛出FaultExceptions。 Decorate your operations to throw FaultExceptions using the FaultContract attribute. 使用FaultContract属性装饰您的操作以引发FaultExceptions。 Now you can catch specific FaultExceptions at the client. 现在,您可以在客户端捕获特定的FaultException。 You should not expose exceptions on the server to the client. 您不应向客户端公开服务器上的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM