You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 6, 2025. It is now read-only.
Is there any "legal" way to wait until the Saga is Completed?
Sometimes need to start Saga and block, for example, UI operation until Saga is Completed or Rejected.
varcoordinator=app.ApplicationServices.GetService<ISagaCoordinator>();varcontext=SagaContext.Create().WithCorrelationId(Guid.NewGuid()).Build();// Sending initial Message/Eventcoordinator.ProcessAsync(newMessage1{Text="Hello"},context);// Desired behavior: block until Saga is Completed or Rejected and return ISagaStatevarstate=coordinator.WaitAsync(context);
Now I can do it with a few stupid lines of code:
// Helper extensionspublicstaticclassSagaExtensions{publicstaticasyncTask<ISagaState>WaitAsync(thisISagaCoordinatorcoordinator,ISagaContextcontext,ISagaStateRepositorysagaStateRepository){varsagaState=awaitsagaStateRepository.GetStateAsync(context);while(sagaStateisnull||sagaState.State==SagaStates.Pending){awaitTask.Delay(TimeSpan.FromSeconds(1));sagaState=awaitsagaStateRepository.GetStateAsync(context);}returnsagaState;}publicstaticasyncTask<ISagaState>GetStateAsync(thisISagaStateRepositorysagaStateRepo,ISagaContextcontext){returnawaitsagaStateRepo.ReadAsync(context.SagaId,(Type)context.Metadata.First(md =>md.Key=="sagaType").Value);}}// Then in some placevarcoordinator=app.ApplicationServices.GetService<ISagaCoordinator>();varcontext=SagaContext.Create().WithSagaId(SagaId.NewSagaId()).WithOriginator("Test").WithMetadata("key","lulz").WithMetadata("sagaType",typeof(SampleSaga)).Build();// Sending initial Message/Eventcoordinator.ProcessAsync(newMessage1{Text="Hello"},context);// Block until Saga is Completed or Rejected and return ISagaStatevarsagaStateRepo=app.ApplicationServices.GetService<ISagaStateRepository>();varstate=awaitcoordinator.WaitAsync(context,sagaStateRepo);if(sagaState.StateisSagaStates.Rejected){// onRejected()}
Is there any "legal" way to wait until the Saga is Completed?
Sometimes need to start Saga and block, for example, UI operation until Saga is Completed or Rejected.
Now I can do it with a few stupid lines of code: