【发布时间】:2018-10-14 05:07:48
【问题描述】:
尝试使用 MT 和 Automatonymous 进行 saga 设置,但是没有点击,文档很薄,不清楚我的配置有什么问题。
我可以看到生成总线的队列,然后是状态机的主题。但是,当我发布事件时,什么也没有发生。没有例外,队列或主题中没有任何内容。
设置和注册:
var repository = new InMemorySagaRepository<MyObJect>();
var _machine = new MyStateMachine();
var _busControl = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
var host = cfg.Host("omitted", h => { });
cfg.UseSerilog(logger);
cfg.ReceiveEndpoint(host, "test", e =>
{
e.StateMachineSaga(_machine, repository);
});
cfg.UseServiceBusMessageScheduler();
});
_busControl.Start();
状态机:
public class MyStateMachine:
MassTransitStateMachine<MyObJect>
{
public MyStateMachine()
{
InstanceState(x => x.CurrentState);
this.Event(() => this.ItemAdded, x => x.CorrelateById(c => c.Message.CorrelationId).SelectId(c => c.Message.CorrelationId));
this.Event(() => this.ItemSubmitted, x => x.CorrelateById(c => c.Message.CorrelationId));
Initially(
When(ItemAdded)
.ThenAsync(context =>
{
return context.Data.AddItem();
//Update everything you need to on the current instance of the state machine
})
.TransitionTo(Added)
);
During(Added,
When(ItemSubmitted)
.Then(ct => ct.Data.SubmitItem())
.TransitionTo(Submitted)
.Finalize()
);
SetCompletedWhenFinalized();
}
public Event<ItemAdded> ItemAdded { get; private set; }
public Event<ItemSubmitted> ItemSubmitted { get; private set; }
public State Added { get; private set; }
public State Submitted { get; private set; }
}
public class ItemSubmitted
{
public string ItemId { get; set; }
public string EntityId { get; set; }
public System.Guid CorrelationId { get; set; }
public void SubmitItem()
{
System.Threading.Thread.Sleep(30000);
Console.WriteLine("Submitted the item");
}
}
public class ItemAdded
{
public string ItemId { get; set; }
public string EntityId { get; set; }
public System.Guid CorrelationId { get; set; }
public Task AddItem()
{
return Task.FromResult(true);
}
}
状态机实例:
public class MyObJect: SagaStateMachineInstance
{
public string CurrentState { get; set; }
public string EntityId { get; set; }
public string ItemId { get; set; }
public Guid CorrelationId { get; set; }
}
通过以下方式发布事件:
_busControl.Publish<MyObJect>(new MyObJect { EntityId = "123", ItemId = "435", CorrelationId = Guid.NewGuid() }).Wait();
请原谅糟糕的代码和人为的例子 - 我把它煮成了一个小安全带,以便更容易地绕开它
【问题讨论】:
标签: masstransit