1

使用 ServiceStack,在两个地方实现异常处理/日志记录很重要:

  1. 每个里面 ServiceRunner<T>

       public class MyServiceRunner<T> : ServiceRunner<T>
       {    
            public override object HandleException(IRequestContext requestContext, T request, Exception ex)
              {
                    // Implement your exception handling/logging here.
                    // T request is your request DTO.
              }
        }
    
  2. 在 AppHost 内部,以便您可以处理服务之外发生的未处理异常。

    public override void Configure(Container container)
    {
        ExceptionHandler = (req, res, operationName, ex) =>
        {
            //Handle Unhandled Exceptions occurring outside of Services
            //E.g. Exceptions during Request binding or in filters:
        }
     }
    

我的问题:

  • 在#1 中,您可以轻松访问请求 DTO(即用于记录目的)。
  • 在处理服务之外发生的异常时,我是否可以访问请求 DTO(或等效的请求有效负载)?
4

1 回答 1

2

在 ServiceStack v4 中,Request DTO 可以从 获得,但您可以使用和IRequest.Dto注册您的 Service 和 Unknown Exception 处理程序IAppHost.ServiceExceptionHandlersIAppHost.UncaughtExceptionHandlers

在 ServiceStack V3 中,您可以注册一个 GlobalRequestFilter,它将请求 DTO 存储在IRequestContext.Items字典中,稍后您可以从请求上下文中获取该字典。

于 2015-12-08T01:31:16.430 回答