【问题标题】:How to catch ServiceStack RequestBindingException如何捕获 ServiceStack RequestBindingException
【发布时间】:2014-03-19 12:59:11
【问题描述】:

我有一个 RequestDto,它接受一个 int 参数

[Route("/club/thread/{Id}","GET")]
public MyDto{
     [Apimember(DataType="int32")]
     public int Id{get;set;}
}

当我输入http://myservice/club/thread/aaa.json时,抛出异常:

[RequestBindingException: 无法绑定请求]

ServiceStack.WebHost.Endpoints.RestHandler.GetRequest(IHttpRequest httpReq, IRestPath restPath) +538

ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName) +1023

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +913

System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

AppHost.ExceptionHandler 无法捕捉到这个异常,如何捕捉它?

【问题讨论】:

    标签: servicestack


    【解决方案1】:

    请求绑定异常是在服务请求上下文之前抛出的未捕获异常,因此由 UncaughtExceptionHandlers 处理,可以在您的 AppHost 中注册:

    public override void Configure(Container container)
    {
        //Custom global uncaught exception handling strategy
        this.UncaughtExceptionHandlers.Add((req, res, operationName, ex) =>
        {
            res.StatusCode = 400;
            res.Write(string.Format("Exception {0}", ex.GetType().Name));
            res.EndRequest(skipHeaders: true);
        });
    }
    

    或者通过覆盖您的 AppHost 的 OnUncaughtException 方法,例如:

    public override void OnUncaughtException(
        IRequest httpReq, IResponse httpRes, string operationName, Exception ex)
    {
        "In OnUncaughtException...".Print();
        base.OnUncaughtException(httpReq, httpRes, operationName, ex);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      相关资源
      最近更新 更多