如何在Saga工作流程中调用多个微服务

时间:2019-01-07 00:41:42

标签: masstransit

我试图在一个传奇工作流类中调用多个微服务。下面是我的代码无法正常工作,请让我知道如何解决它。非常感谢您的帮助。

如果我通过付款消费者方法发布消息,则能够使通知服务正常工作。但我想从传奇工作流中调用我的通知依赖类。

public class PaymentSaga : MassTransitStateMachine<PaymentSagaStatus>,IPayment
{
        public PaymentSaga()
        {
            InstanceState(s => s.CurrentState);

            Event(() => PaymentCollectCommand, x =>
            x.CorrelateBy(pay => pay.StudentId.ToString(),
                context => context.Message.StudentId.ToString())
            .SelectId(context => Guid.NewGuid()));

            // Payment
            Event(() => PaymentCollectedEvent, x => x.CorrelateById(context =>
                context.Message.CorrelationId));

            // Notification
            Event(() => NotificationEvent, x => x.CorrelateById(context =>
                context.Message.CorrelationId));

            Initially(
                When(PaymentCollectCommand)
                .Then(context =>
                {
                    // Will be done by auto mapper                    
                    context.Instance.ReceivedPaymentDate = DateTime.Now;
                    context.Instance.StudentId = context.Data.StudentId;
                    context.Instance.CourseName = context.Data.CourseName;
                    context.Instance.Amount = context.Data.Amount;
                    context.Instance.TransactionDate = context.Data.TransactionDate;
                })
                // Send to payment Service
                .ThenAsync(
                    context => Console.Out.WriteLineAsync(
                        $"Student payment received. Id:{context.Instance.CorrelationId}"))
                    .TransitionTo(ReceivePayment)
                    .Publish(context => new PaymentReceivedEvent(context.Instance))                                
                );

            During(ReceivePayment,
                When(PaymentCollectedEvent)
                    .Then(context => context.Instance.ReceivedPaymentDate = DateTime.Now)
                    .ThenAsync(
                        context => Console.Out.WriteLineAsync(
                            $"Student payment processed. Id: {context.Instance.CorrelationId}")),

                When(NotificationEvent)
                    .Then(context =>
                    {
                        // Will be done by auto mapper                    
                        context.Instance.ReceivedPaymentDate = DateTime.Now;
                        context.Instance.Message = context.Data.Message;
                    })
                    .ThenAsync(context => Console.Out.WriteLineAsync(
                        $"Notification send. Id: {context.Instance.CorrelationId}"))
                    .TransitionTo(NotificationReceived)
                    .Publish(context => new NotificationReceivedEvent(context.Instance))
                    .Finalize());            
            SetCompletedWhenFinalized();
        }

        public State ReceivePayment { get; private set; }
        public State NotificationReceived { get; private set; }

        public Event<IPayment> PaymentCollectCommand { get; private set; }
        public Event<IPaymentReceivedEvent> PaymentCollectedEvent { get; private set; }
        public Event<INotification> NotificationEvent { get; private set; }

        public int StudentId { get; set; }
        public string CourseName { get; set; }
        public DateTime TransactionDate { get; set; }
        public double Amount { get; set; }

        public string Message { get; set; }
}

public class PaymentReceivedEvent : IPaymentReceivedEvent
{
    private readonly PaymentSagaStatus _instance;

    public PaymentReceivedEvent(PaymentSagaStatus instance)
    {
        _instance = instance;
    }

    public Guid CorrelationId => _instance.CorrelationId;
    public int StudentId => _instance.StudentId;
    public string CourseName => _instance.CourseName;
    public DateTime TransactionDate => _instance.TransactionDate;
    public double Amount => _instance.Amount;
}

public class NotificationReceivedEvent : INotification
{
    private readonly PaymentSagaStatus _instance;

    public NotificationReceivedEvent(PaymentSagaStatus instance)
    {
        _instance = instance;
    }

    public Guid CorrelationId => _instance.CorrelationId;

    public string Message => $"{_instance.StudentId} is being enrolled in {_instance.CourseName}. Fee paid: £{_instance.Amount}";
}

0 个答案:

没有答案