我们使用带有自动匿名和 InMemoryRepository 的 Masstransit 来实现 saga 持久性。我们配置了大约 3 个状态机并且运行良好。我们最近从 InMemoryRepository 更改为 EFCore 以实现持久性。这导致只有第一个配置的状态机可以完美运行。休息所有状态机甚至没有进入 Initially 事件。需要帮助来了解实施是否正确。以下是代码详细信息:
大众运输状态机设置
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 =>
{
AddConsumers(e, provider);
e.ConfigureSaga<ServiceRequest1RegisterState>(provider);
e.ConfigureSaga<ServiceRequest1UpdateState>(provider);
e.ConfigureSaga<ServiceRequest1ApproveState>(provider);
});
}));
////x.AddSagaStateMachine<ServiceRequest1RegisterStateMachine, ServiceRequest1RegisterState>()
//// .InMemoryRepository();
////x.AddSagaStateMachine<ServiceRequest1UpdateStateMachine, ServiceRequest1UpdateState>()
//// .InMemoryRepository();
////x.AddSagaStateMachine<ServiceRequest1ApproveStateMachine, ServiceRequest1ApproveState>()
//// .InMemoryRepository();
x.AddSagaStateMachine<ServiceRequest1RegisterStateMachine, ServiceRequest1RegisterState>()
.EntityFrameworkRepository(r =>
{
r.ConcurrencyMode = ConcurrencyMode.Pessimistic; // or use Optimistic, which requires RowVersion
r.AddDbContext<DbContext, ServiceRequest1RegisterStateDbContext>((provider, builder) =>
{
builder.UseSqlServer(configuration.GetConnectionString("StateDBConnection"), m =>
{
m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
m.MigrationsHistoryTable($"__{nameof(ServiceRequest1RegisterStateDbContext)}");
});
});
});
x.AddSagaStateMachine<ServiceRequest1UpdateStateMachine, ServiceRequest1UpdateState>()
.EntityFrameworkRepository(r =>
{
r.ConcurrencyMode = ConcurrencyMode.Pessimistic; // or use Optimistic, which requires RowVersion
r.AddDbContext<DbContext, ServiceRequest1UpdateStateDbContext>((provider, builder) =>
{
builder.UseSqlServer(configuration.GetConnectionString("StateDBConnection"), m =>
{
m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
m.MigrationsHistoryTable($"__{nameof(ServiceRequest1UpdateStateDbContext)}");
});
});
});
x.AddSagaStateMachine<ServiceRequest1ApproveStateMachine, ServiceRequest1ApproveState>()
.EntityFrameworkRepository(r =>
{
r.ConcurrencyMode = ConcurrencyMode.Pessimistic; // or use Optimistic, which requires RowVersion
r.AddDbContext<DbContext, ServiceRequest1ApproveStateDbContext>((provider, builder) =>
{
builder.UseSqlServer(configuration.GetConnectionString("StateDBConnection"), m =>
{
m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
m.MigrationsHistoryTable($"__{nameof(ServiceRequest1ApproveStateDbContext)}");
});
});
});
});
services.AddSingleton<IHostedService, MassTransitBusService>();
所有 3 个状态机的 DBContext 如下所示
public class ServiceRequest1ApproveStateDbContext : SagaDbContext
{
public ServiceRequest1ApproveStateDbContext(DbContextOptions<ServiceRequest1ApproveStateDbContext> options)
: base(options)
{
}
/// <summary>
/// Gets the configurations.
/// </summary>
protected override IEnumerable<ISagaClassMap> Configurations
{
get { yield return new ServiceRequest1ApproveStateMap(); }
}
}
下面的状态图
public class ServiceRequest1ApproveStateMap : SagaClassMap<ServiceRequest1ApproveState>
{
protected override void Configure(EntityTypeBuilder<ServiceRequest1ApproveState> entity, ModelBuilder model)
{
entity.Property(x => x.CurrentState).HasMaxLength(64);
entity.Property(x => x.Id);
entity.Property(x => x.ServiceId);
entity.Property(x => x.ReadyEventStatus);
}
}
从上面的代码中,只有 ServiceRequest1RegisterStateMachine 可以正常工作,其余的状态机甚至都没有进入 Initially。我可以确认迁移都是事先完成的,并且当仅存在 1 个存储库配置时,这些状态机都可以单独正常工作,即,如果只有 ServiceRequest1ApproveStateMachine 的 repo 配置,则此状态机可以正常工作。但是如果所有 3 个 repo 配置都存在,那么只有第一个有效。我需要有关使用 EFCore 正确实现 saga 持久性的指导。我是否应该尝试使用 masstransit 文档中提到的单个 dbcontext 来实现(https://masstransit-project.com/usage/sagas/efcore.html)