我使用最新版本的Rebus(0.99.35)與SimpleInjector(3.1.2)一起。在我的第一個示例項目中,我將SQL Server用於傳輸和Sagas。Rebus佐賀大火IAmInitiatedBy多次
問題是,佐賀方法Handle(StartTheSagaMessage message)
,執行IAmInitiatedBy<StartTheSagaMessage>
,被稱爲5次,我不明白爲什麼。此外,這種方法公佈了一條從未被公交車接收到的信息。
下面是配置代碼:
var container = new Container();
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(i => i.FullName.StartsWith("Messages"));
container.RegisterCollection(typeof(IHandleMessages<>), assemblies);
var bus = Configure.With(new SimpleInjectorContainerAdapter(container))
.Logging(l => l.Trace())
.Transport(t => t.UseSqlServer(connectionstring, "Messages", "consumer"))
.Routing(r => r.TypeBased().MapAssemblyOf<Job>("consumer"))
.Sagas(s => s.StoreInSqlServer(connectionstring, "Sagas", "SagaIndexTable"))
.Options(o =>
{
o.SetNumberOfWorkers(1);
o.SetMaxParallelism(1);
})
.Start();
container.Verify();
bus.Subscribe<Step1FinishedMessage>().Wait();
bus.Subscribe<Step2FinishedMessage>().Wait();
var procId = Guid.NewGuid();
bus.Send(new StartTheSagaMessage() { ProcessId = procId });
而佐賀代碼:
public class MySaga : Saga<MySagaData>,
IAmInitiatedBy<StartTheSagaMessage>,
IHandleMessages<Step1FinishedMessage>,
IHandleMessages<Step2FinishedMessage>
{
public IBus Bus { get; set; }
protected override void CorrelateMessages(ICorrelationConfig<MySagaData> config)
{
config.Correlate<StartTheSagaMessage>(m => m.ProcessId, s => s.SagaProcessId);
config.Correlate<Step1FinishedMessage>(m => m.ProcessId, s => s.SagaProcessId);
config.Correlate<Step2FinishedMessage>(m => m.ProcessId, s => s.SagaProcessId);
}
public async Task Handle(StartTheSagaMessage message)
{
if (IsNew == false)
return;
Trace.TraceInformation("Mysaga - got StartTheSagaMessage: {0}", message.ProcessId);
//The saga is started - Do some stuff - call webservices (in external handler)
//When this step is finished the external process replies with a "step1FinishedMessage"
this.Data.SagaProcessId = message.ProcessId;
//Fake Step1FinishMessage (should be replied from external handler)
await Bus.Send(new Step1FinishedMessage() { ProcessId = this.Data.SagaProcessId });
}
public async Task Handle(Step1FinishedMessage message)
{
Trace.TraceInformation("Mysaga - got Step1FinishedMessage: {0}", message.ProcessId);
//Sagabehaviour when the Step1 is finished by the external handler
this.Data.Step1Finished = true;
//After dalying 10 seconds - Send a step2finishedmessage
await Bus.Defer(TimeSpan.FromSeconds(10), new Step2FinishedMessage() { ProcessId = this.Data.SagaProcessId });
}
public async Task Handle(Step2FinishedMessage message)
{
await Task.Run(() =>
//return Task.FromResult<void>(() =>
{
Trace.TraceInformation("Mysaga - got Step2FinishedMessage: {0}", message.ProcessId);
//Step2 is handled - finished the saga
this.Data.Step2Finished = true;
this.MarkAsComplete();
});
}
}
的全樣本是基於solution available here。
我在做什麼錯了?
感謝您的幫助。
*此處提供的解決方案*使用Autofac? – qujck
是的。源代碼解決方案也使用舊的nuget包。 – ilcorvo
是的,我可以看到,更新所有軟件包會生成無法編譯的代碼。 – qujck