我正在將一個舊應用程式移植到 .net6,并遇到了使用 masstransit 注冊多個 sagas 的問題。
services.AddMassTransit<IProcessManagerBus>(busCfg =>
{
busCfg.AddSagaStateMachine<OrderPM, OrderPMState>()
.EntityFrameworkRepository<OrderPMState>(efConfig =>
{
efConfig.ConcurrencyMode = ConcurrencyMode.Optimistic;
efConfig.DatabaseFactory(() => new OrderStateDbContext(configuration.GetConnectionString("DB")));
});
busCfg.AddSagaStateMachine<CsaLoginPM, CsaLoginPMState>()
.EntityFrameworkRepository<CsaLoginPMState>(efConfig =>
{
efConfig.ConcurrencyMode = ConcurrencyMode.Optimistic;
efConfig.DatabaseFactory(() => new CsaLoginStateDbContext(configuration.GetConnectionString("DB")));
});
busCfg.UsingRabbitMq((context, rabbitCfg) =>
{
rabbitCfg.UseJsonSerializer();
rabbitCfg.Host(new Uri(configuration.GetValue<string>("messaging:pm-bus:host-address")), hostCfg =>
{
hostCfg.Username(configuration.GetValue<string>("messaging:pm-bus:username"));
hostCfg.Password(configuration.GetValue<string>("messaging:pm-bus:password"));
rabbitCfg.ReceiveEndpoint(configuration.GetValue<string>("messaging:pm-bus:receive-queue"), epCfg =>
{
epCfg.PrefetchCount = 10;
epCfg.UseRetry(retryConfig => retryConfig.Exponential(5, TimeSpan.FromMilliseconds(500), TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(1)));
epCfg.ConfigureSagas(context);
});
});
});
});
OrderPMState 作業正常,但 CsaLoginPMState 在觸發時會給出以下錯誤:System.InvalidOperationException:物體型別 CsaLoginPMState 不是當前背景關系模型的一部分。
如果我注釋掉 OrderPMState 的注冊,則 CsaLoginPMState 作業正常。我懷疑這 2 個 sagas 使用的是相同的 DbContext,盡管它們是在各自的 DbContext 中注冊的。
OrderStateDbContext
public class OrderStateDbContext : SagaDbContext
{
public OrderStateDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
protected override IEnumerable<ISagaClassMap> Configurations
{
get { yield return new OrderPMStateMapping(); }
}
}
CsaLoginStateDbContext
public class CsaLoginStateDbContext : SagaDbContext
{
public CsaLoginStateDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
protected override IEnumerable<ISagaClassMap> Configurations
{
get { yield return new CsaLoginPMStateMapping(); }
}
}
舊版本的應用程式使用 AutoFac,注冊是這樣完成的:
builder.Register(x =>
EntityFrameworkSagaRepository<OrderPMState>.CreateOptimistic(() => new OrderStateDbContext(_configuration.GetConnectionString("DB"))))
.As<ISagaRepository<OrderPMState>>().SingleInstance();
builder.Register(x =>
EntityFrameworkSagaRepository<CsaLoginPMState>.CreateOptimistic(() => new CsaLoginStateDbContext(_configuration.GetConnectionString("DB"))))
.As<ISagaRepository<CsaLoginPMState>>().SingleInstance();
我錯過了什么嗎?
uj5u.com熱心網友回復:
使用 時DbContext,您應該使用專為與 一起使用而設計的兩種配置方法之一DbContext。
busCfg.AddSagaStateMachine<CsaLoginPM, CsaLoginPMState>()
.EntityFrameworkRepository<CsaLoginPMState>(efConfig =>
{
efConfig.ConcurrencyMode = ConcurrencyMode.Optimistic;
efConfig.AddDbContext<DbContext, CsaLoginStateDbContext>((provider,builder) =>
{
builder.UseSqlServer(configuration.GetConnectionString("DB"), m =>
{
m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
m.MigrationsHistoryTable($"__{nameof(CsaLoginStateDbContext)}");
});
});
});
或者您可以單獨添加 DbContext 并在 saga 中使用現有的 DbContext:
services.AddDbContext<CsaLoginStateDbContext>(builder =>
builder.UseSqlServer(configuration.GetConnectionString("DB"), m =>
{
m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
m.MigrationsHistoryTable($"__{nameof(CsaLoginStateDbContext)}");
}));
services.AddMassTransit(x =>
{
x.AddSagaRepository<JobSaga>()
.EntityFrameworkRepository(r =>
{
r.ConcurrencyMode = ConcurrencyMode.Optimistic;
r.ExistingDbContext<CsaLoginStateDbContext>();
});
});
這在檔案中都有介紹。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/431237.html
上一篇:ListAPIView與quersetUser.objects.all()在測驗資料庫中只有兩個用戶進行7次查詢?
下一篇:EF核心過濾
