2016-03-03 39 views
1

我使用最新版本的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

我在做什麼錯了?

感謝您的幫助。

+0

*此處提供的解決方案*使用Autofac? – qujck

+0

是的。源代碼解決方案也使用舊的nuget包。 – ilcorvo

+0

是的,我可以看到,更新所有軟件包會生成無法編譯的代碼。 – qujck

回答

1

我改變了佐賀,比它的作品。

我把:

private IBus _bus; 

    public MySaga(IBus bus) 
    { 
     _bus = bus; 
    } 

在地方:

public IBus Bus { get; set; } 

然後它的作品!我不明白爲什麼,在調試方法中調試Bus不是null。

+0

5個調用表明一定有錯誤。你應該看看日誌,看看有什麼不對。如果您登錄到跟蹤輸出,您應該配置某種跟蹤偵聽器,將日誌管理到某個文件的某個位置 - 或者(我推薦這麼做),可以使用其中一個很好的.NET日誌記錄庫。優秀的[Serilog](https://github.com/serilog/serilog) – mookid8000

+0

不幸的是,日誌中沒有消息。謝謝你對Serilog的建議。 – ilcorvo