我正在使用MassTransit Sagas。当传入事件(AccountCreatedInWebAppEvent)进行一些处理并开始发布时,它将开始另一个事件(SagaAccountCreatedEvent)
我有一个消费者,它在侦听SagaAccountCreatedEvent,但是在消费者中context.CorrelationId为null,如果我没有按照下面的示例进行显式设置(返回新的SagaAccountCreatedEvent(){...})。由Saga-magic增添了我无需将其插入消息中就应该包含的correlationId吗? (从消费者到Saga-“处理程序”的发布也是如此)
InstanceState(i => i.CurrentState);
Event(() => AccountCreatedInWebAppEvent, e => e.CorrelateBy<int>(sagaState => sagaState.AccountId, context => context.Message.Account.AccountId)
.SelectId(context => Guid.NewGuid()))
...
Initially(
When(AccountCreatedInWebAppEvent)
.Then(context =>
{
context.Instance.AccountId = context.Data.Account.AccountId;
context.Instance.CreatedInWebApp = DateTime.Now;
})
.TransitionTo(AccountCreatedSentOut)
.Publish(context =>
{
return new SagaAccountCreatedEvent() { Account = context.Data.Account, CorrelationId = context.CorrelationId.Value };
})
消费方法
公共异步任务消耗(ConsumeContext上下文){
//here context.correlationId is null
}
由于您的初始事件与int
属性相关,因此您正在使用SelectId(x => Guid.NewGuid())
初始化实例。要在发布后续事件时获取该值,应使用context.Instance.CorrelationId
,如下所示:
.Publish(context =>
new SagaAccountCreatedEvent()
{
Account = context.Data.Account,
CorrelationId = context.Instance.CorrelationId
})
您正在使用的值,context.CorrelationId
来自传奇的消费上下文,并且仅在Guid使用CorrelationId直接关联的事件中存在。