コード例 #1
0
        public async Task HandleAsync(MessageEvent messageEvent)
        {
            // https://stackoverflow.com/questions/43124722/application-insights-custom-trackrequest-is-creating-duplicate-messages
            var requestTelemetry = new RequestTelemetry
            {
                Name = "CheckOutEventHandler invoked from EventBus"
            };

            _telemetryClient.TrackRequest(requestTelemetry);

            string correlationToken = null;

            try
            {
                correlationToken = messageEvent.CorrelationToken;

                var checkedOutEvent = messageEvent as CheckOutEvent;

                _logger.LogInformation($"Invoked CheckOutEventHandler in Ordering.API for Request {correlationToken} ");

                _telemetryClient.TrackEvent(
                    $"Event: Invoked CheckOutEventHandler in Ordering.API for Request {correlationToken}");

                if (checkedOutEvent == null)
                {
                    _logger.LogError(
                        $"Publishing EmptyBasketEvent from CheckOutEventHandler in Ordering.API for Request {correlationToken} ");
                    throw new Exception(
                              $"Exception in CheckOutEventHandler: 'CheckOutEvent is Null' for Request {correlationToken}");
                }

                _telemetryClient.TrackEvent(
                    $"Event: CheckOutEventHandler invoked: BasketID:{checkedOutEvent.OrderInformationModel.BasketId}");

                var createOrderCommand = new CreateOrderCommand(
                    checkedOutEvent.OrderInformationModel.BasketId,
                    checkedOutEvent.OrderInformationModel.CheckoutId,
                    checkedOutEvent.OrderInformationModel.Buyer.Username,
                    checkedOutEvent.OrderInformationModel.Total,
                    checkedOutEvent.OrderInformationModel.Buyer.FirstName,
                    checkedOutEvent.OrderInformationModel.Buyer.LastName,
                    checkedOutEvent.OrderInformationModel.Buyer.Address,
                    checkedOutEvent.OrderInformationModel.Buyer.City,
                    checkedOutEvent.OrderInformationModel.Buyer.State,
                    checkedOutEvent.OrderInformationModel.Buyer.PostalCode,
                    checkedOutEvent.OrderInformationModel.Buyer.Phone,
                    checkedOutEvent.OrderInformationModel.Buyer.Email,
                    checkedOutEvent.OrderInformationModel.Payment.CreditCardNumber,
                    checkedOutEvent.OrderInformationModel.Payment.SecurityCode,
                    checkedOutEvent.OrderInformationModel.Payment.CardholderName,
                    checkedOutEvent.OrderInformationModel.Payment.ExpirationDate,
                    checkedOutEvent.CorrelationToken = correlationToken,
                    // Is this cool Linq? Generated from Resharper. It iterates through lineItem collection and projects an orderDetailDto for each item
                    // Map collection of CheckOutEventLineItems to collection of OderDetailDtos
                    checkedOutEvent.OrderInformationModel.LineItems.Select(lineItem => new OrderDetailDto
                {
                    Artist    = lineItem.Artist,
                    Title     = lineItem.Title,
                    Quantity  = lineItem.Quantity,
                    UnitPrice = decimal.Parse(lineItem.UnitPrice),
                    AlbumId   = lineItem.ProductId
                }).ToList()
                    );

                // Invoke Command that creates order
                var orderId = await _orderCommandHandler.Handle(createOrderCommand);

                checkedOutEvent.OrderInformationModel.OrderSystemId = orderId;

                _telemetryClient.TrackEvent(
                    $"Event: CheckOutEventHandler: Buyer created:{orderId}");

                //************** Publish Event  *************************
                // Publish event to clear basket for this order from Basket service
                var emptyCartEvent = new EmptyBasketEvent
                {
                    BasketID         = checkedOutEvent.OrderInformationModel.BasketId,
                    CorrelationToken = correlationToken
                };

                _logger.LogInformation(
                    $"Publishing EmptyBasketEvent from CheckOutEventHandler in Ordering.API for Request {correlationToken} ");

                _telemetryClient.TrackEvent(
                    $"Event: Publishing EmptyBasketEvent from CheckOutEventHandler for orderid:{orderId}");

                await _eventBusPublisher.Publish <EmptyBasketEvent>(emptyCartEvent);
            }
            catch (Exception ex)
            {
                throw new Exception(
                          $"Exception in CheckOutEventHandler: {ex.Message} for Request {correlationToken}");
            }

            await Task.CompletedTask;
        }
コード例 #2
0
        public async Task HandleAsync(Message message)
        {
            try
            {
                // Interrogate the message
                // Get the Event Type
                message.UserProperties.TryGetValue("Event", out var eventType);

                // Get the CorrelationToken
                message.UserProperties.TryGetValue("correlationToken", out var correlationToken);

                // Get the message body
                var body = Encoding.UTF8.GetString(message.Body);

                // Deserialize the message body into the event class
                var checkOut = JsonConvert.DeserializeObject <UserCheckoutEvent>(body);

                // Create Command (data) object that follows simple CQRS pattern
                var createOrderCommand = new CreateOrderCommand(
                    checkOut.CustomerId,
                    checkOut.BasketId,
                    checkOut.OrderDate,
                    checkOut.UserName,
                    checkOut.Total,
                    checkOut.OrderDetails);

                // Invoke Command that creates order
                var orderId = await _orderCommandHandler.Handle(createOrderCommand);

                // Create Buyer Command (data) object that follows simple CQRS pattern
                var createBuyerCommand = new CreateBuyerCommand(orderId,
                                                                checkOut.UserName,
                                                                checkOut.BuyerInformation.FirstName,
                                                                checkOut.BuyerInformation.LastName,
                                                                checkOut.BuyerInformation.Address,
                                                                checkOut.BuyerInformation.City,
                                                                checkOut.BuyerInformation.State,
                                                                checkOut.BuyerInformation.PostalCode,
                                                                checkOut.BuyerInformation.Country,
                                                                checkOut.BuyerInformation.Phone,
                                                                checkOut.BuyerInformation.Email);

                // Invoke Command that creates buyer
                await _buyerCommandHandler.Handle(createBuyerCommand);

                //************** Publish Event  *************************
                // Publish event to clear basket for this order from Basket service
                var emptyCartEvent = new EmptyBasketEvent
                {
                    BasketID         = checkOut.BasketId,
                    CorrelationToken = correlationToken?.ToString()
                };

                await _eventBus.Publish(emptyCartEvent,
                                        MessageEventEnum.InvokeEmptyBasketEvent,
                                        correlationToken?.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception($"Exception executing UserCheckoutEvent in Eventhandler : {ex.Message}");
            }

            await Task.CompletedTask;
        }