2013-02-28 75 views
1

在我的web应用程序我利用在Global.asax中的Application_Error功能来记录所有的异常,像这样:如何在WCF REST服务添加全局错误处理

void Application_Error(object sender, EventArgs e) 
{ 
    Exception ex = Server.GetLastError(); 

    while (ex.GetBaseException() != null) 
    { 
     ex = ex.GetBaseException(); 
    } 

    log.writeError(ex.ToString()); 
} 

我在WCF尝试similiar REST服务没有运气。我将如何添加全局错误处理?我看到了this article,但我是实施IServiceBehavior的新手。我在哪里添加上面的代码?

回答

3

我使用:

1)AppDomain.CurrentDomain.UnhandledException事件

2)TaskScheduler.UnobservedTaskException事件

3)IErrorHandler:

public class ErrorHandler : IErrorHandler 
    { 
     public void ProvideFault(Exception error, MessageVersion version, ref Message fault) 
     { 
      var faultException = new FaultException<string>("Server error: " + error.Format()); 
      var messageFault = faultException.CreateMessageFault(); 
      fault = Message.CreateMessage(version, messageFault, null); 
     } 

     public bool HandleError(Exception error) 
     { 
      return false; 
      //return true; //if handled 
     } 
    } 

[AttributeUsage(AttributeTargets.Class)] 
public class ErrorHandlerBehavior : Attribute, IEndpointBehavior, IServiceBehavior 
{ 
    public void Validate(ServiceEndpoint endpoint) 
    { 

    } 

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
    { 

    } 

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
    { 
     endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandler()); 
    } 

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
    { 

    } 

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 

    } 

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) 
    { 

    } 

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers) 
     { 
      channelDispatcher.ErrorHandlers.Add(new ErrorHandler()); 
     } 
    } 
} 

这可以应用到整个服务impl。类:

[ErrorHandlerBehavior] 
    public class SubscriberInfoTaskService : {} 

或端点:

var endpoint = Host.Description.Endpoints.FirstOrDefault(); 

//foreach (ChannelDispatcher channelDispatcher in Host.ChannelDispatchers) //ChannelDispatcherBase 
//{     
// channelDispatcher.ErrorHandlers.Add(new ErrorHandler()); 
//} 

endpoint.Behaviors.Add(new ErrorHandlerBehavior()); 

这里关于使用配置:http://www.steverb.com/post/2008/11/24/Useful-WCF-Behaviors-IErrorHandler.aspx

+0

我怎样才能让 “op.ParameterInspectors.Add(新ValidatingParameterInspector());”在线编译?我是否需要继承其他课程? – user1886415 2013-02-28 15:54:37

+0

对不起,你不需要这个。我删除了这些行。 – SalientBrain 2013-02-28 16:20:09

+0

感谢上帝!这终于奏效了。我一直在使用Google搜索整个上午,没有一篇文章显示100%清晰的解决方案。你是我的英雄人,刚刚救了我的一天:) – WtFudgE 2016-04-11 04:35:24