Когда отладчик дошёл до этой точки, он переходит в thenAsync и после этого не возвращается к основному контроллеру (BurgerController), происходит долгая загрузка, что приводит к таймауту запроса. Я думаю, что-то не так или не возвращается из SagaCompletionNotification.
During(FinishedCooking,
When(BurgerCookerFinishedCookingEvent)
.ThenAsync(async x =>
{
//should final response back to Main controller
await x.Publish(new SagaCompletionNotification
{
CorrelationId = x.Saga.CorrelationId
});
})
.TransitionTo(Completed)
.Finalize()
);
Завершить SagaStateMachine
public class BurgerCookStateMachine : MassTransitStateMachine
{
public State Ordered { get; private set; }
public State BeginCooking { get; private set; }
public State FinishedCooking { get; private set; }
public State Completed { get; private set; }
public Event BurgerCookerOrderedEvent { get; private set; }
public Event BurgerCookerBeginCookingEvent { get; private set; }
public Event BurgerCookerFinishedCookingEvent { get; private set; }
public BurgerCookStateMachine()
{
InstanceState(x => x.CurrentState);
Event(() => BurgerCookerOrderedEvent, x => x.CorrelateById(context => context.Message.CorrelationId));
Event(() => BurgerCookerBeginCookingEvent, x => x.CorrelateById(context => context.Message.CorrelationId));
Event(() => BurgerCookerFinishedCookingEvent, x => x.CorrelateById(context => context.Message.CorrelationId));
Initially(
When(BurgerCookerOrderedEvent)
.ThenAsync(async x =>
{
x.Saga.CustomerName = x.Message.CustomerName;
})
.PublishAsync(async x => new BurgerCookerBeginCookingEvent()
{
CorrelationId = x.Saga.CorrelationId,
CookTemp = x.Saga.CookTemp, //check x.message.cookTemp
})
.TransitionTo(BeginCooking)
);
During(BeginCooking,
When(BurgerCookerBeginCookingEvent)
.ThenAsync(async x =>
{
//make more functionality
})
.PublishAsync(async x=>new CookBurger
{
CookTemp = "90Degree",
CorrelationId = x.Saga.CorrelationId
})
.TransitionTo(FinishedCooking));
During(FinishedCooking,
When(BurgerCookerFinishedCookingEvent)
.ThenAsync(async x =>
{
//should final response back to Main controller
await x.Publish(new SagaCompletionNotification
{
CorrelationId = x.Saga.CorrelationId
});
})
.TransitionTo(Completed)
.Finalize()
);
SetCompletedWhenFinalized();
}
}
потребители
public class CookBurgerConsumer : IConsumer
{
public async Task Consume(ConsumeContext context)
{
if (context.Message.CookTemp.Equals("90Degree"))
{
await Task.Delay(2000);
}
await context.Publish(new BurgerCookerFinishedCookingEvent()
{
CorrelationId = context.Message.CorrelationId
});
}
}
Контроллер
public class BurgerController : ControllerBase
{
private readonly IRequestClient _requestClient;
public BurgerController(IRequestClient requestClient)
{
_requestClient = requestClient;
}
[HttpPost("burgerOrder")]
public async Task PlaceBurgerOrder()
{
var order = new BurgerCookerOrderedEvent()
{
CorrelationId = Guid.NewGuid(),
CustomerName = "JOHN-BOB",
CookTemp = "MED"
};
var response = await _requestClient.GetResponse(order);
if (response.Message != null)
{
return Ok("Burger Cooked Successfully");
}
return StatusCode(500, "Burger cooking process failed");
}
}
СОБЫТИЯ
namespace Order.Application.Saga.BurgerSaga
{
public class BurgerCookerState : SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }
public int CurrentState { get; set; }
public string CustomerName { get; set; }
public string CookTemp { get; set; }
}
public record BurgerCookerOrderedEvent
{
public Guid CorrelationId { get; set; }
public string CustomerName { get; set; }
public string CookTemp { get; set; }
}
public record BurgerCookerBeginCookingEvent
{
public Guid CorrelationId { get; set; }
public string CustomerName { get; set; }
public string CookTemp { get; set; }
}
public record BurgerCookerFinishedCookingEvent
{
public Guid CorrelationId { get; set; }
}
public class SagaCompletionNotification
{
public Guid CorrelationId { get; set; }
}
public class CookBurger
{
public string CookTemp { get; set; }
public Guid CorrelationId { get; set; }
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... g-resulted