public Task Handle(string requestId, dynamic vm, RouteData routeData, HttpRequest request) { /* * In a production environment if multiple services are interested in the * same post request the handling logic is much more complex than what we * are doing in this demo. In this demo both Finance and Reservations need * to handle the POST to /reservations/reserve. The implementation assumes * that the host/infrastructure never fails, which is not the case in a * production environment. In order to make this part safe, which is not the * scope of this demo asynchronous messaging should be introduced earlier in * the processing pipeline. * * More information: https://milestone.topics.it/2019/05/02/safety-first.html */ var message = new ReserveTicket() { TicketId = int.Parse((string)routeData.Values["id"]), ReservationId = new Guid(request.Cookies["reservation-id"]) }; /* * WARN: destination is hard-coded to reduce demo complexity. * In a production environment routing should be configured * at startup by the host/infrastructure. */ return(messageSession.Send("Reservations.Service", message)); }
public async Task Handle(ReserveTicket message, IMessageHandlerContext context) { Console.WriteLine($"Adding ticket '{message.TicketId}' to reservation '{message.ReservationId}'.", Color.Green); Data.ReservationId = message.ReservationId; /* * Tickets are reserved indefinitely. Usually tickets booking websites * Allow people to reserve a ticket, or keep it in the shopping cart for * a fixed amount of time, e.g. 10 minutes. After 10 minutes if tickets * are not purchased they are automatically released back to the pool * of available tickets. This can be easily achieved using a timeout in * this reservation saga. The timeout should be kicked of once the first * time a ticket is reserved. If the timeout expires and the reservation * is still existing all reserved tickets can be released. */ await context.SendLocal(new MarkTicketAsReserved() { ReservationId = message.ReservationId, TicketId = message.TicketId }); Console.WriteLine($"MarkTicketAsReserved request sent.", Color.Green); }