Jasonyi
Jasonyi

Reputation: 531

How to catch ServiceStack RequestBindingException

i have a RequestDto,it accept a int parameter

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

when i input http://myservice/club/thread/aaa.json ,is throw a exception:

[RequestBindingException: Unable to bind request]

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 can't catch this exception,how can i catch it ?

Upvotes: 1

Views: 2005

Answers (1)

mythz
mythz

Reputation: 143319

Request binding exceptions is an Uncaught exception that is thrown before the context of a service request so gets handled by UncaughtExceptionHandlers which can be registered in your 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);
    });
}

Or by overriding your AppHost's OnUncaughtException method, e.g:

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

Upvotes: 4

Related Questions