连接到 Mongo DB 的 Masstransit Saga Class 中的异常
Exception in Masstransit Saga Class which is connected to Mongo DB
下面是我在 saga class 中遇到的错误 "MassTransitStateMachine"。我不确定为什么会出现此错误,因为当我切换到内存传奇存储库时它正在工作。错误:MassTransit.Messages 警告:0:R-RETRY rabbitmq://localhost/IMembersLoginSaga 7c2d0000-8af1-d8cb-0f3a-08d6e7b9f5b1 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance., System.FormatException: 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance.MassTransit.Messages 错误:0 : R-FAULT rabbitmq://localhost/IMembersLoginSaga 7c2d0000-8af1-d8cb-0f3a-08d6e7b9f5b1 ct.component.abstraction.Consumer.Members.IMemberLoginResponse ct.members.domain.Models.MemberLoginStateInstance(00:00:00.0045448) 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance., System.FormatException: 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance。
InstanceState(s => s.CurrentState);
Event(() => Create, x =>
x.CorrelateById(ml => ml.CorrelationId, context => context.Message.CorrelationId));
Event(() => Response, x => x.CorrelateById(context => context.Message.CorrelationId));
Initially(
When(Create)
.Then(context =>
{
context.Instance.CorrelationId = context.Data.CorrelationId;
context.Instance.EmailAddress = context.Data.EmailAddress;
context.Instance.Password = context.Data.Password;
context.Instance.Created = DateTime.Now;
})
.ThenAsync(context => Console.Out.WriteLineAsync($"member email is {context.Instance.EmailAddress}"))
.Publish(context => new MemberloginPublish(context.Instance))
.TransitionTo(Approve)
);
During(Approve,
When(Response)
.Then(context =>
{
context.Instance.CorrelationId = context.Data.CorrelationId;
context.Instance.Token = context.Data.Token;
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Token: {context.Data.Token} received"))
.Finalize());
SetCompletedWhenFinalized();
private Event<IMembersLoginSaga> Create { get; set; }
private Event<IMemberLoginResponse> Response { get; set; }
private State Approve { get; set; }
Startup class
opt.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri(MasstransitSettings["Server"]), hostConfigurator =>
{
hostConfigurator.Username(MasstransitSettings["UserName"]);
hostConfigurator.Password(MasstransitSettings["Password"]);
});
cfg.ReceiveEndpoint(host, nameof(IMembersLoginSaga), ep =>
{
ep.PrefetchCount = 16;
ep.UseMessageRetry(r => r.Interval(2, 100));
ep.ConfigureConsumers(context);
ep.StateMachineSaga(new MemberLoginStateMachine(),
new MongoDbSagaRepository<MemberLoginStateInstance>(dbconnection,new MongoDbSagaConsumeContextFactory())
//new InMemorySagaRepository<MemberLoginStateInstance>()
);
});
}));
修饰一个 mongoDB 属性,它是 saga 机器状态实例 class 中 correlationId 上的 BsonId()。这将解决问题
下面是我在 saga class 中遇到的错误 "MassTransitStateMachine"。我不确定为什么会出现此错误,因为当我切换到内存传奇存储库时它正在工作。错误:MassTransit.Messages 警告:0:R-RETRY rabbitmq://localhost/IMembersLoginSaga 7c2d0000-8af1-d8cb-0f3a-08d6e7b9f5b1 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance., System.FormatException: 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance.MassTransit.Messages 错误:0 : R-FAULT rabbitmq://localhost/IMembersLoginSaga 7c2d0000-8af1-d8cb-0f3a-08d6e7b9f5b1 ct.component.abstraction.Consumer.Members.IMemberLoginResponse ct.members.domain.Models.MemberLoginStateInstance(00:00:00.0045448) 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance., System.FormatException: 元素 '_id' 不匹配任何字段或 属性 of class ct.members.domain.Models.MemberLoginStateInstance。
InstanceState(s => s.CurrentState);
Event(() => Create, x =>
x.CorrelateById(ml => ml.CorrelationId, context => context.Message.CorrelationId));
Event(() => Response, x => x.CorrelateById(context => context.Message.CorrelationId));
Initially(
When(Create)
.Then(context =>
{
context.Instance.CorrelationId = context.Data.CorrelationId;
context.Instance.EmailAddress = context.Data.EmailAddress;
context.Instance.Password = context.Data.Password;
context.Instance.Created = DateTime.Now;
})
.ThenAsync(context => Console.Out.WriteLineAsync($"member email is {context.Instance.EmailAddress}"))
.Publish(context => new MemberloginPublish(context.Instance))
.TransitionTo(Approve)
);
During(Approve,
When(Response)
.Then(context =>
{
context.Instance.CorrelationId = context.Data.CorrelationId;
context.Instance.Token = context.Data.Token;
})
.ThenAsync(context => Console.Out.WriteLineAsync($"Token: {context.Data.Token} received"))
.Finalize());
SetCompletedWhenFinalized();
private Event<IMembersLoginSaga> Create { get; set; }
private Event<IMemberLoginResponse> Response { get; set; }
private State Approve { get; set; }
Startup class
opt.AddBus(context => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri(MasstransitSettings["Server"]), hostConfigurator =>
{
hostConfigurator.Username(MasstransitSettings["UserName"]);
hostConfigurator.Password(MasstransitSettings["Password"]);
});
cfg.ReceiveEndpoint(host, nameof(IMembersLoginSaga), ep =>
{
ep.PrefetchCount = 16;
ep.UseMessageRetry(r => r.Interval(2, 100));
ep.ConfigureConsumers(context);
ep.StateMachineSaga(new MemberLoginStateMachine(),
new MongoDbSagaRepository<MemberLoginStateInstance>(dbconnection,new MongoDbSagaConsumeContextFactory())
//new InMemorySagaRepository<MemberLoginStateInstance>()
);
});
}));
修饰一个 mongoDB 属性,它是 saga 机器状态实例 class 中 correlationId 上的 BsonId()。这将解决问题