使用自动机名称时状态机不一致



我是微服务的新手,并使用带有automonymous的masstransit。目前,我的状态机在执行时显示不一致。内部的代码最初按预期工作,但在第二个事件完成后,控制流不会执行内部的代码。这种情况只发生在一个状态机上,其他所有状态机都按预期工作。当我将During中的代码移到Initial(最初在中注释了代码(时,它工作得很好。下面是我的状态机的样子:

InstanceState(x => x.CurrentState);
Event(() => ServiceRequest1Registered, x => x.CorrelateById(context => context.Message.AggregateId));
Event(() => ServiceRequest2Registered, x => x.CorrelateById(context => context.Message.AggregateId));
Initially(
When(ServiceRequest1Registered, 
context => context.Data.ServiceTypeId != (int)ServiceType.IndividualService)
.Then(context => _logger.LogInformation($"When Initially, ServiceRequest1Registered and wrong condition"))
.Finalize(),
When(ServiceRequest1Registered, 
context => context.Data.ServiceTypeId == (int)ServiceType.IndividualService)
.Then(context => _logger.LogInformation($"When Initially and ServiceRequest1Registered"))
.Send(url,
x => new ServiceRequest2RegisteredCommand
{
InitiatedBy = x.Instance.InitiatedBy,
ServiceRequestId = x.Instance.Id,
Schedules = _mapper.Map<List<ScheduleDTO>>(x.Data.ServiceRequest1Schedules)
})
.Then(context => _logger.LogInformation($"Send ServiceRequest2RegisteredCommand")
.TransitionTo(ServiceRequest1RegisterCompleted)
////When(ServiceRequest2Registered)
////.Then(context => _logger.LogInformation($"When ServiceRequestRegister2Completed and ServiceRequest2Registered"))
//// .Finalize())
);
During(ServiceRequestRegister1Completed,
Ignore(ServiceRequest1Registered),
When(ServiceRequest2Registered)
.Then(context => _logger.LogInformation($"When ServiceRequestRegister1Completed and ServiceRequest2Registered"))
.Finalize());
SetCompleted(async instance =>
{
State<ServiceRequestState> currentState = await this.GetState(instance);
_logger.LogInformation($"Final state : {ServiceRequest2Registered.Equals(currentState)}");
return ServiceRequest2Registered.Equals(currentState);
});

使用低于的RabbitMQ设置Masstransit

services.AddMassTransit(x =>
{
x.AddBus(provider => MassTransit.Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(hostUri, hst =>
{
hst.Username(appSettings.RabbitMQ.Username);
hst.Password(appSettings.RabbitMQ.Password);
});
cfg.ReceiveEndpoint("microservice-response", e =>
{
e.UseInMemoryOutbox();
AddConsumers(e, provider);
e.ConfigureSaga<ServiceRequestRegisterState>(provider);
});
}));
x.AddSagaStateMachine<ServiceRequestRegisterStateMachine, ServiceRequestRegisterState>()
.InMemoryRepository();
});
services.AddSingleton<IHostedService, MassTransitBusService>();

我同时尝试了SetCompleted和SetCompletedWhenFinalized((。我们使用的是带有automonymous v4.2.1的Masstransit v6.2.1。需要帮助来确定我们为什么会出现这个问题,或者实施是否有问题?

如果在初始事件完成之前观察到传递到状态机的事件,则应将UseInMemoryOutbox添加到接收端点(在状态机配置之前(。这将推迟出站消息,直到状态机实例被持久化。我怀疑您使用的是乐观锁定策略,第二个事件在初始事件持久性完成之前到达。

第二个事件的Correlation Id不同。这导致第二个事件最初发生。纠正了那个问题,其余都起作用了。确保状态机中的所有事件都具有相同的correlationid,并且此correlationid在编排过程中不会更改。

相关内容

  • 没有找到相关文章

最新更新