我想在.net核心项目中使用masstransit saga,但是我找不到任何文档或(最佳实践)示例来处理异常。
如果使用者中有错误,应该发布一些失败的事件并加以使用。实际上,我认为这不是一个好的解决方案。所以我想抛出异常并赶上Masstransit Statemachine。
OrderReceivedConsumer.cs
public class OrderReceivedConsumer : IConsumer<IOrderReceivedEvent>
{
public async Task Consume(ConsumeContext<IOrderReceivedEvent> context)
{
var orderCommand = context.Message;
await Console.Out.WriteLineAsync($"Order code: {orderCommand.OrderCode} Order id: {orderCommand.OrderId} is received.");
//do something..
throw new Exception("test");
//await context.Publish<IOrderProcessedEvent>(
// new { CorrelationId = context.Message.CorrelationId, OrderId = orderCommand.OrderId });
}
}
OrderSaga.cs
public class OrderSaga : MassTransitStateMachine<OrderSagaState>
{
public State Received { get; set; }
public State Processed { get; set; }
public Event<IOrderCommand> OrderCommand { get; set; }
public Event<IOrderProcessedEvent> OrderProcessed { get; set; }
public OrderSaga()
{
InstanceState(s => s.CurrentState);
Event(() => OrderCommand,
cec =>
cec.CorrelateBy(state => state.OrderCode, context => context.Message.OrderCode)
.SelectId(selector => Guid.NewGuid()));
Event(() => OrderProcessed, cec => cec.CorrelateById(selector =>
selector.Message.CorrelationId));
Initially(
When(OrderCommand)
.Then(context =>
{
context.Instance.OrderCode = context.Data.OrderCode;
context.Instance.OrderId = context.Data.OrderId;
})
.ThenAsync(
context => Console.Out.WriteLineAsync($"{context.Data.OrderId} order id is received..")
)
.TransitionTo(Received)
.Publish(context => new OrderReceivedEvent(context.Instance))
);
During(Received,
When(OrderProcessed)
.ThenAsync(
context => Console.Out.WriteLineAsync($"{context.Data.OrderId} order id is processed.."))
.Finalize()
);
SetCompletedWhenFinalized();
}
}
我应该在哪里使用自动命名库的“捕获”方法?