从 MassTransit 故障消息中获取异常属性
Get Exception properties from MassTransit Fault message
我希望能够访问故障消费者内部的消费者抛出的原始异常的属性。例如,如果未处理的异常是一个包含错误集合的 ValidationException
,我是否能够从故障使用者访问该集合?
我似乎唯一可以访问的是 ExceptionType 和 Message。我想我可以解析异常消息以获取错误集合,但是有没有一种方法可以在不解析消息和生成集合的情况下实现这一点?
public async Task Consume(ConsumeContext<Fault<MyMessage>> context)
{
string exceptionType = context.Message.Exceptions[0].ExceptionType;
string exceptionMessage = context.Message.Exceptions[0].Message;
if (exceptionType == "FluentValidation.ValidationException")
{
// here I want to get the Errors collection on the exception of type ValidationException
}
}
MassTransit 不会序列化 Exception
,它会将异常详细信息封装在 Fault
事件中包含的 ExceptionInfo
类型中。
无法访问原始 Exception
类型,这是有充分理由的。在我看来,将异常序列化为消息契约的一部分只是一种不好的做法。
我希望能够访问故障消费者内部的消费者抛出的原始异常的属性。例如,如果未处理的异常是一个包含错误集合的 ValidationException
,我是否能够从故障使用者访问该集合?
我似乎唯一可以访问的是 ExceptionType 和 Message。我想我可以解析异常消息以获取错误集合,但是有没有一种方法可以在不解析消息和生成集合的情况下实现这一点?
public async Task Consume(ConsumeContext<Fault<MyMessage>> context)
{
string exceptionType = context.Message.Exceptions[0].ExceptionType;
string exceptionMessage = context.Message.Exceptions[0].Message;
if (exceptionType == "FluentValidation.ValidationException")
{
// here I want to get the Errors collection on the exception of type ValidationException
}
}
MassTransit 不会序列化 Exception
,它会将异常详细信息封装在 Fault
事件中包含的 ExceptionInfo
类型中。
无法访问原始 Exception
类型,这是有充分理由的。在我看来,将异常序列化为消息契约的一部分只是一种不好的做法。