我是微服务的新手,并且使用自动匿名的公共交通。目前我的状态机在执行时显示不一致。Initially 内部的代码按预期工作,但在第二个事件完成后,控制流不执行 during 内部的代码。这只发生在 1 个状态机上,所有其他状态机都按预期工作。当我将代码中的代码移动到最初(最初注释代码)时,它工作正常。下面是我的状态机的样子:
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()。我们将 Masstransit v6.2.1 与自动 v4.2.1 一起使用。需要帮助来确定我们为什么会遇到此问题或实施是否有问题?