How do I finalize a class based Saga, so that it is deleted from the SQL database (EF Core)?

烂漫一生 提交于 2019-12-01 11:41:42

问题


I am trying to learn more about Mass Transit as we are thinking about adopting it. I now have the class based Saga below, which works as expected:

public class EchoSaga : ISaga,
        InitiatedBy<TextEntered>,
        Orchestrates<TextEchoStart>,
        Orchestrates<EchoEnd>
    {
        public Guid CorrelationId { get; set; }
        public string CurrentState { get; set; }
        public string Text { get; set; }

        public Task Consume(ConsumeContext<TextEntered> context)
        {
            CurrentState = "Entered";
            Text = context.Message.Text;
            return Task.CompletedTask;
        }

        public Task Consume(ConsumeContext<TextEchoStart> context)
        {
            CurrentState = "Start";
            Text = context.Message.Text;
            return Task.CompletedTask;
        }

        public Task Consume(ConsumeContext<EchoEnd> context)
        {
            CurrentState = "End";
            Text = context.Message.Text;
            return Task.CompletedTask;
        }
    }

A class based Saga is different to a state machine Saga and is described more in the docs here in the Saga section: http://masstransit-project.com/MassTransit/understand/key-ideas.html.

How do I mark the Saga as finalized after EchoEnd is consumed so it is deleted from the database (I have already setup the repository)? If I was using a state machine Saga, then I could do this:

.Finalize()
.SetCompletedWhenFinalized()

How do I do this with a class based Saga?

I realise I may be going backwards a bit here, however I am trying to learn how Mass Transit started and where it is now to see if it meets our requirements. Very pleased with it so far.


回答1:


If you cast the ConsumeContext to SagaConsumeContext<TSaga, TMessage>, there is a SetCompleted method which signals the saga is complete and can be removed from the repository.

You may need to use context.GetPayload<SagaConsumeContext<TSaga, TMessage>>() if casting directly doesn't work (due to proxy use in the pipeline).



来源:https://stackoverflow.com/questions/56516412/how-do-i-finalize-a-class-based-saga-so-that-it-is-deleted-from-the-sql-databas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!