使用 Agatha Framework 的异常处理 (BusinessExceptionType)

Exception Handling with Agatha Framework (BusinessExceptionType)

我正在使用带有自定义异常的 Agatha 框架。我有一个例外,MyException,我在配置我的请求时将其指定为 BusinessExceptionType

new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(SomeRequestResponse.MakeARequest).Assembly, typeof(Container)) { BusinessExceptionType = typeof(MyException) }.Initialize();

这就是我抛出异常的方式:

throw new MyException(message);

然而,当抛出异常时,MyException 不被识别为 BusinessExceptionType。如何让它被识别为 BusinessExceptionType?以下条件不成立。

if (response.ExceptionType == ExceptionType.Business)

这里是例外 class:

public class MyException : Exception
{
    public MyException()
    {

    }

    public MyException(string message) : base(message)
    {

    }

    public MyException(string message, Exception inner) : base(message, inner)
    {

    }

    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context)
    {

    }
}

更新: 看起来 BusinessExceptionTypeserviceLayerConfiguration 中设置正确,直到我在 BusinessExceptionType 变为 null 时发出我的第一个请求。在配置请求和发出我的第一个请求之间的某个地方,它变成了 null。要么,要么它在 RequestProcessor.cs 中抓取不同的 serviceLayerConfiguration。对此的任何帮助表示赞赏。

我发现你必须在抛出之前在响应中设置异常类型。

return new SomeRequestResponse.MakeAResponse() { Exception = new Exception(ex), ExceptionType = ExceptionType.Business };

然后在您的消费者中,此声明将为真:

if (response.ExceptionType == ExceptionType.Business)

如果您想自己例外,也可以这样做:

return new MakeARequestResponse.MakeAResponse()
{
    Exception = new ExceptionInfo(new Exception("Whatever you want to say.")),
    ExceptionType = ExceptionType.Business
};