0

servicestack 的 ExceptionHandler(在 AppHostBase 的重写配置方法中设置)在 lambda 中具有通用异常类型的“异常”参数。

this.ExceptionHandler = (httpReq, httpResp, operationName, exception) =>
{
    if(exception is ArgumentException)
    {
      // some code
    }
}

ArgumentException在 lambda 内部,如果异常是类型,我希望添加一个特定条件。有什么方法可以确定引发了哪种特定类型的异常?使用 'is' 关键字检查类型不起作用,如此链接所示

仅供参考,为我们使用的 servicestack 实例实现了自定义 ServiceRunner。

4

1 回答 1

0

导致的那段代码ArgumentException

return serializer.Deserialize(querystring, TEMP);

由于某种原因,无法将异常对象识别为ArgumentException内部ExceptionHandler

this.ExceptionHandler = (httpReq, httpResp, operationName, exception) =>
{
    httpResp.StatusCode = 500;
    bool isArgEx = exception is ArgumentException; // returns false        
    if(isArgEx)
    {
        //do something
    }
}

虽然,如链接中所述(请参阅问题),可以使用关键字识别InnerException 。is

因此,应用的解决方案是将 抛出ArgumentException为内部异常,如下所示:

public const string ARG_EX_MSG = "Deserialize|ArgumentException";

try
{
    return serializer.Deserialize(querystring, TEMP);
}
catch(ArgumentException argEx)
{
    throw new Exception(ARG_EX_MSG, argEx);
}

因此,现在的ExceptionHandler代码是:

this.ExceptionHandler = (httpReq, httpResp, operationName, exception) =>
{
    httpResp.StatusCode = 500;
    bool isArgEx = exception.InnerException is ArgumentException; // returns true
    if(isArgEx)
    {
        //do something
    }
}
于 2017-06-23T12:16:01.203 回答