From cf44ba2fd8987085706ffd011d16d4a5512b4e0b Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 14 Mar 2019 11:59:57 +0800 Subject: [PATCH] Refactor Ordering Api eventbus using CAP --- .../Application/Behaviors/LoggingBehavior.cs | 2 +- .../Behaviors/TransactionBehaviour.cs | 8 +- .../Behaviors/ValidatorBehavior.cs | 2 +- .../Commands/CreateOrderCommandHandler.cs | 2 +- .../Commands/IdentifiedCommandHandler.cs | 2 +- ...ePeriodConfirmedIntegrationEventHandler.cs | 21 +-- ...derPaymentFailedIntegrationEventHandler.cs | 23 +-- ...rPaymentSuccededIntegrationEventHandler.cs | 23 +-- ...erStockConfirmedIntegrationEventHandler.cs | 23 +-- ...derStockRejectedIntegrationEventHandler.cs | 22 +-- ...CheckoutAcceptedIntegrationEventHandler.cs | 19 +- .../GracePeriodConfirmedIntegrationEvent.cs | 4 +- .../OrderPaymentFailedIntegrationEvent .cs | 4 +- .../OrderPaymentSuccededIntegrationEvent.cs | 4 +- .../Events/OrderStartedIntegrationEvent.cs | 10 +- ...gedToAwaitingValidationIntegrationEvent.cs | 3 +- ...tatusChangedToCancelledIntegrationEvent.cs | 10 +- ...rderStatusChangedToPaidIntegrationEvent.cs | 3 +- ...rStatusChangedToShippedIntegrationEvent.cs | 10 +- ...ChangedToStockConfirmedIntegrationEvent.cs | 4 +- ...tatusChangedTosubmittedIntegrationEvent.cs | 10 +- .../OrderStockConfirmedIntegrationEvent.cs | 4 +- .../OrderStockRejectedIntegrationEvent.cs | 3 +- .../UserCheckoutAcceptedIntegrationEvent.cs | 5 +- .../IOrderingIntegrationEventService.cs | 6 +- .../OrderingIntegrationEventService.cs | 56 +----- .../Controllers/OrdersController.cs | 2 +- .../Extensions/GenericTypeExtensions.cs | 30 ++++ .../AutofacModules/ApplicationModule.cs | 5 +- ...131634_IntegrationEventInitial.Designer.cs | 43 ----- .../20170330131634_IntegrationEventInitial.cs | 34 ---- ...IntegrationEventLogContextModelSnapshot.cs | 42 ----- .../Ordering/Ordering.API/Ordering.API.csproj | 10 +- src/Services/Ordering/Ordering.API/Program.cs | 4 +- src/Services/Ordering/Ordering.API/Startup.cs | 164 +++++------------- .../OrderingScenarioBase.cs | 4 +- .../Services/Catalog/CatalogScenariosBase.cs | 4 +- .../Ordering/OrderingScenariosBase.cs | 4 +- 38 files changed, 162 insertions(+), 467 deletions(-) create mode 100644 src/Services/Ordering/Ordering.API/Extensions/GenericTypeExtensions.cs delete mode 100644 src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.Designer.cs delete mode 100644 src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.cs delete mode 100644 src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/IntegrationEventLogContextModelSnapshot.cs diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs index 35023b297..268d854ff 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/LoggingBehavior.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging; using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; +using Ordering.API.Extensions; namespace Ordering.API.Application.Behaviors { diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs index d9d3e0b0a..efcfccb21 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs @@ -1,13 +1,12 @@ using MediatR; using Microsoft.EntityFrameworkCore; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; using Microsoft.Extensions.Logging; -using Ordering.API.Application.IntegrationEvents; using Serilog.Context; using System; using System.Threading; using System.Threading.Tasks; +using Ordering.API.Extensions; namespace Ordering.API.Application.Behaviors { @@ -15,14 +14,11 @@ namespace Ordering.API.Application.Behaviors { private readonly ILogger> _logger; private readonly OrderingContext _dbContext; - private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public TransactionBehaviour(OrderingContext dbContext, - IOrderingIntegrationEventService orderingIntegrationEventService, ILogger> logger) { _dbContext = dbContext ?? throw new ArgumentException(nameof(OrderingContext)); - _orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentException(nameof(orderingIntegrationEventService)); _logger = logger ?? throw new ArgumentException(nameof(ILogger)); } @@ -53,8 +49,6 @@ namespace Ordering.API.Application.Behaviors await _dbContext.CommitTransactionAsync(transaction); } - - await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(); }); return response; diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs index 6fc12258b..abd781446 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/ValidatorBehavior.cs @@ -5,7 +5,7 @@ using Ordering.Domain.Exceptions; using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; +using Ordering.API.Extensions; namespace Ordering.API.Application.Behaviors { diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs index 00a088c09..184678330 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs @@ -56,7 +56,7 @@ _logger.LogInformation("----- Creating Order - Order: {@Order}", order); _orderRepository.Add(order); - + return await _orderRepository.UnitOfWork .SaveEntitiesAsync(); } diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/IdentifiedCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/IdentifiedCommandHandler.cs index e389e3975..d24033917 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/IdentifiedCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/IdentifiedCommandHandler.cs @@ -1,5 +1,4 @@ using MediatR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; using Microsoft.Extensions.Logging; using Ordering.API.Application.Behaviors; @@ -7,6 +6,7 @@ using Ordering.API.Application.Commands; using System; using System.Threading; using System.Threading.Tasks; +using Ordering.API.Extensions; namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands { diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs index 2e003b322..7d30f2ad6 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/GracePeriodConfirmedIntegrationEventHandler.cs @@ -1,18 +1,16 @@ using MediatR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.API; -using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Microsoft.Extensions.Logging; using Ordering.API.Application.Behaviors; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationEvents.Events; using Serilog.Context; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.API.Application.IntegrationEvents.EventHandling { - public class GracePeriodConfirmedIntegrationEventHandler : IIntegrationEventHandler + public class GracePeriodConfirmedIntegrationEventHandler : ICapSubscribe { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -30,24 +28,15 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling /// has been completed and order will not initially be cancelled. /// Therefore, the order process continues for validation. /// - /// - /// - /// + //TODO: [CapSubscribe(nameof(GracePeriodConfirmedIntegrationEvent))] public async Task Handle(GracePeriodConfirmedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - command.GetGenericTypeName(), - nameof(command.OrderNumber), - command.OrderNumber, - command); - await _mediator.Send(command); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs index a123dd891..9577faa4e 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -1,20 +1,17 @@ -namespace Ordering.API.Application.IntegrationEvents.EventHandling +using DotNetCore.CAP; + +namespace Ordering.API.Application.IntegrationEvents.EventHandling { using MediatR; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.API; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Microsoft.Extensions.Logging; - using Ordering.API.Application.Behaviors; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationEvents.Events; using Serilog.Context; using System.Threading.Tasks; using System; - public class OrderPaymentFailedIntegrationEventHandler : - IIntegrationEventHandler + public class OrderPaymentFailedIntegrationEventHandler : ICapSubscribe { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -27,21 +24,15 @@ _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + //TODO: [CapSubscribe(nameof(OrderPaymentFailedIntegrationEvent))] public async Task Handle(OrderPaymentFailedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var command = new CancelOrderCommand(@event.OrderId); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - command.GetGenericTypeName(), - nameof(command.OrderNumber), - command.OrderNumber, - command); - await _mediator.Send(command); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs index 9cc69e5e8..71cdf3335 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -1,20 +1,17 @@ -namespace Ordering.API.Application.IntegrationEvents.EventHandling +using DotNetCore.CAP; + +namespace Ordering.API.Application.IntegrationEvents.EventHandling { using MediatR; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.API; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Microsoft.Extensions.Logging; - using Ordering.API.Application.Behaviors; using Ordering.API.Application.Commands; using Ordering.API.Application.IntegrationEvents.Events; using Serilog.Context; using System; using System.Threading.Tasks; - public class OrderPaymentSuccededIntegrationEventHandler : - IIntegrationEventHandler + public class OrderPaymentSuccededIntegrationEventHandler : ICapSubscribe { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -27,21 +24,15 @@ _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + //TODO: [CapSubscribe(nameof(OrderPaymentSuccededIntegrationEvent))] public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var command = new SetPaidOrderStatusCommand(@event.OrderId); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - command.GetGenericTypeName(), - nameof(command.OrderNumber), - command.OrderNumber, - command); - await _mediator.Send(command); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs index 6438b01d0..3dda7db55 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -1,20 +1,17 @@ -namespace Ordering.API.Application.IntegrationEvents.EventHandling +using DotNetCore.CAP; + +namespace Ordering.API.Application.IntegrationEvents.EventHandling { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using System.Threading.Tasks; using Events; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using MediatR; using System; using Ordering.API.Application.Commands; using Microsoft.Extensions.Logging; using Serilog.Context; using Microsoft.eShopOnContainers.Services.Ordering.API; - using Ordering.API.Application.Behaviors; - public class OrderStockConfirmedIntegrationEventHandler : - IIntegrationEventHandler + public class OrderStockConfirmedIntegrationEventHandler :ICapSubscribe { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -27,21 +24,15 @@ _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + //TODO: [CapSubscribe(nameof(OrderStockConfirmedIntegrationEvent))] public async Task Handle(OrderStockConfirmedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var command = new SetStockConfirmedOrderStatusCommand(@event.OrderId); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - command.GetGenericTypeName(), - nameof(command.OrderNumber), - command.OrderNumber, - command); - await _mediator.Send(command); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs index b457211ed..67fd80bac 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs @@ -1,19 +1,17 @@ -namespace Ordering.API.Application.IntegrationEvents.EventHandling +using DotNetCore.CAP; + +namespace Ordering.API.Application.IntegrationEvents.EventHandling { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using System.Threading.Tasks; using Events; using System.Linq; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using MediatR; using Ordering.API.Application.Commands; using Microsoft.Extensions.Logging; using Serilog.Context; using Microsoft.eShopOnContainers.Services.Ordering.API; - using Ordering.API.Application.Behaviors; - public class OrderStockRejectedIntegrationEventHandler : IIntegrationEventHandler + public class OrderStockRejectedIntegrationEventHandler :ICapSubscribe { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -26,11 +24,12 @@ _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } + //TODO: [CapSubscribe(nameof(OrderStockRejectedIntegrationEvent))] public async Task Handle(OrderStockRejectedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var orderStockRejectedItems = @event.OrderStockItems .FindAll(c => !c.HasStock) @@ -39,13 +38,6 @@ var command = new SetStockRejectedOrderStatusCommand(@event.OrderId, orderStockRejectedItems); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - command.GetGenericTypeName(), - nameof(command.OrderNumber), - command.OrderNumber, - command); - await _mediator.Send(command); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs index a1452b23c..6d8d8e1b4 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -1,18 +1,16 @@ using MediatR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.Extensions.Logging; -using Ordering.API.Application.Behaviors; using Ordering.API.Application.IntegrationEvents.Events; using Serilog.Context; using System.Threading.Tasks; using System; +using DotNetCore.CAP; namespace Ordering.API.Application.IntegrationEvents.EventHandling { - public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler + public class UserCheckoutAcceptedIntegrationEventHandler : ICapSubscribe { private readonly IMediator _mediator; private readonly ILogger _logger; @@ -33,12 +31,12 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling /// basket.api once it has successfully process the /// order items. /// - /// + //TODO: [CapSubscribe(nameof(UserCheckoutAcceptedIntegrationEvent))] public async Task Handle(UserCheckoutAcceptedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var result = false; @@ -53,13 +51,6 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling var requestCreateOrder = new IdentifiedCommand(createOrderCommand, @event.RequestId); - _logger.LogInformation( - "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", - requestCreateOrder.GetGenericTypeName(), - nameof(requestCreateOrder.Id), - requestCreateOrder.Id, - requestCreateOrder); - result = await _mediator.Send(requestCreateOrder); if (result) diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs index 15b0aebb5..d027843a2 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Ordering.API.Application.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent + public class GracePeriodConfirmedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs index fec066521..f9d0c1f0b 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs @@ -1,8 +1,6 @@ namespace Ordering.API.Application.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderPaymentFailedIntegrationEvent : IntegrationEvent + public class OrderPaymentFailedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs index 778aa8114..5d94b7565 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Ordering.API.Application.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent + public class OrderPaymentSuccededIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs index 4a5d7db38..d22ee5db9 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs @@ -1,15 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { // Integration Events notes: // An Event is “something that has happened in the past”, therefore its name has to be // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems. - public class OrderStartedIntegrationEvent : IntegrationEvent + public class OrderStartedIntegrationEvent { public string UserId { get; set; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs index a104215af..0f0e11ff2 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs @@ -1,9 +1,8 @@ namespace Ordering.API.Application.IntegrationEvents.Events { using System.Collections.Generic; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToAwaitingValidationIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs index 6ff92a1d3..c65d5b32f 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs @@ -1,12 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { - public class OrderStatusChangedToCancelledIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToCancelledIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs index 6bcbc3494..55802f168 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs @@ -1,9 +1,8 @@ namespace Ordering.API.Application.IntegrationEvents.Events { using System.Collections.Generic; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToPaidIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs index 1753fcc78..88e44e69c 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs @@ -1,12 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { - public class OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToShippedIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs index 24e51f55b..069e40f03 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Ordering.API.Application.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToStockConfirmedIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedTosubmittedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedTosubmittedIntegrationEvent.cs index 816c8bddf..5d6da9f8f 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedTosubmittedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedTosubmittedIntegrationEvent.cs @@ -1,12 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { - public class OrderStatusChangedToSubmittedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToSubmittedIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockConfirmedIntegrationEvent.cs index 4bcc9aab5..0ff633891 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockConfirmedIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Ordering.API.Application.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderStockConfirmedIntegrationEvent : IntegrationEvent + public class OrderStockConfirmedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs index 647970581..8b8d3162c 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs @@ -1,9 +1,8 @@ namespace Ordering.API.Application.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using System.Collections.Generic; - public class OrderStockRejectedIntegrationEvent : IntegrationEvent + public class OrderStockRejectedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs index 2cb49aae0..f0612fe25 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/UserCheckoutAcceptedIntegrationEvent.cs @@ -1,10 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using Ordering.API.Application.Models; +using Ordering.API.Application.Models; using System; namespace Ordering.API.Application.IntegrationEvents.Events { - public class UserCheckoutAcceptedIntegrationEvent : IntegrationEvent + public class UserCheckoutAcceptedIntegrationEvent { public string UserId { get; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs index 05e8f0e4f..8f5b7c19f 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs @@ -1,11 +1,9 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace Ordering.API.Application.IntegrationEvents { public interface IOrderingIntegrationEventService { - Task PublishEventsThroughEventBusAsync(); - Task AddAndSaveEventAsync(IntegrationEvent evt); + Task AddAndSaveEventAsync(object evt); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs index 9d85e2dd4..16da65037 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs @@ -1,72 +1,34 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Storage; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Utilities; -using Microsoft.eShopOnContainers.Services.Ordering.API; +using Microsoft.EntityFrameworkCore.Storage; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; using Microsoft.Extensions.Logging; using System; -using System.Data.Common; -using System.Diagnostics; -using System.Linq; using System.Threading.Tasks; +using DotNetCore.CAP; +using Ordering.API.Extensions; namespace Ordering.API.Application.IntegrationEvents { public class OrderingIntegrationEventService : IOrderingIntegrationEventService { - private readonly Func _integrationEventLogServiceFactory; - private readonly IEventBus _eventBus; + private readonly ICapPublisher _eventBus; private readonly OrderingContext _orderingContext; - private readonly IntegrationEventLogContext _eventLogContext; - private readonly IIntegrationEventLogService _eventLogService; private readonly ILogger _logger; - public OrderingIntegrationEventService(IEventBus eventBus, + public OrderingIntegrationEventService(ICapPublisher eventBus, OrderingContext orderingContext, - IntegrationEventLogContext eventLogContext, - Func integrationEventLogServiceFactory, ILogger logger) { _orderingContext = orderingContext ?? throw new ArgumentNullException(nameof(orderingContext)); - _eventLogContext = eventLogContext ?? throw new ArgumentNullException(nameof(eventLogContext)); - _integrationEventLogServiceFactory = integrationEventLogServiceFactory ?? throw new ArgumentNullException(nameof(integrationEventLogServiceFactory)); _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); - _eventLogService = _integrationEventLogServiceFactory(_orderingContext.Database.GetDbConnection()); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public async Task PublishEventsThroughEventBusAsync() + public async Task AddAndSaveEventAsync(object evt) { - var pendindLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync(); + _logger.LogInformation("----- Enqueuing integration event to repository ({@IntegrationEvent})", evt); - foreach (var logEvt in pendindLogEvents) - { - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", logEvt.EventId, Program.AppName, logEvt.IntegrationEvent); - - try - { - await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId); - _eventBus.Publish(logEvt.IntegrationEvent); - await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId); - } - catch (Exception ex) - { - _logger.LogError(ex, "ERROR publishing integration event: {IntegrationEventId} from {AppName}", logEvt.EventId, Program.AppName); - - await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId); - } - } - } - - public async Task AddAndSaveEventAsync(IntegrationEvent evt) - { - _logger.LogInformation("----- Enqueuing integration event {IntegrationEventId} to repository ({@IntegrationEvent})", evt.Id, evt); - - await _eventLogService.SaveEventAsync(evt, _orderingContext.GetCurrentTransaction.GetDbTransaction()); + _eventBus.Transaction.Begin(_orderingContext.GetCurrentTransaction.GetDbTransaction()); + await _eventBus.PublishAsync(evt.GetGenericTypeName(), evt); } } } diff --git a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs index ae35b0377..cbd4c2070 100644 --- a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs +++ b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs @@ -1,7 +1,6 @@ using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services; @@ -12,6 +11,7 @@ using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; +using Ordering.API.Extensions; namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers { diff --git a/src/Services/Ordering/Ordering.API/Extensions/GenericTypeExtensions.cs b/src/Services/Ordering/Ordering.API/Extensions/GenericTypeExtensions.cs new file mode 100644 index 000000000..ad3a73e99 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Extensions/GenericTypeExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Linq; + +namespace Ordering.API.Extensions +{ + public static class GenericTypeExtensions + { + public static string GetGenericTypeName(this Type type) + { + var typeName = string.Empty; + + if (type.IsGenericType) + { + var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray()); + typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>"; + } + else + { + typeName = type.Name; + } + + return typeName; + } + + public static string GetGenericTypeName(this object @object) + { + return @object.GetType().GetGenericTypeName(); + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs index dbda8bc14..39e4d8f2c 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs @@ -1,5 +1,4 @@ using Autofac; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; @@ -42,8 +41,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof .As() .InstancePerLifetimeScope(); - builder.RegisterAssemblyTypes(typeof(CreateOrderCommandHandler).GetTypeInfo().Assembly) - .AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); + //builder.RegisterAssemblyTypes(typeof(CreateOrderCommandHandler).GetTypeInfo().Assembly) + // .AsClosedTypesOf(typeof(IIntegrationEventHandler<>)); } } diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.Designer.cs b/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.Designer.cs deleted file mode 100644 index 65b6a6e05..000000000 --- a/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.Designer.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; - -namespace Ordering.API.Infrastructure.IntegrationEventMigrations -{ - [DbContext(typeof(IntegrationEventLogContext))] - [Migration("20170330131634_IntegrationEventInitial")] - partial class IntegrationEventInitial - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { - modelBuilder - .HasAnnotation("ProductVersion", "1.1.1") - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.IntegrationEventLogEntry", b => - { - b.Property("EventId") - .ValueGeneratedOnAdd(); - - b.Property("Content") - .IsRequired(); - - b.Property("CreationTime"); - - b.Property("EventTypeName") - .IsRequired(); - - b.Property("State"); - - b.Property("TimesSent"); - - b.HasKey("EventId"); - - b.ToTable("IntegrationEventLog"); - }); - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.cs b/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.cs deleted file mode 100644 index 9830ebf7b..000000000 --- a/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/20170330131634_IntegrationEventInitial.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Ordering.API.Infrastructure.IntegrationEventMigrations -{ - public partial class IntegrationEventInitial : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "IntegrationEventLog", - columns: table => new - { - EventId = table.Column(nullable: false), - Content = table.Column(nullable: false), - CreationTime = table.Column(nullable: false), - EventTypeName = table.Column(nullable: false), - State = table.Column(nullable: false), - TimesSent = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_IntegrationEventLog", x => x.EventId); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "IntegrationEventLog"); - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/IntegrationEventLogContextModelSnapshot.cs b/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/IntegrationEventLogContextModelSnapshot.cs deleted file mode 100644 index 29bd24729..000000000 --- a/src/Services/Ordering/Ordering.API/Infrastructure/IntegrationEventMigrations/IntegrationEventLogContextModelSnapshot.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; - -namespace Ordering.API.Infrastructure.IntegrationEventMigrations -{ - [DbContext(typeof(IntegrationEventLogContext))] - partial class IntegrationEventLogContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { - modelBuilder - .HasAnnotation("ProductVersion", "1.1.1") - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.IntegrationEventLogEntry", b => - { - b.Property("EventId") - .ValueGeneratedOnAdd(); - - b.Property("Content") - .IsRequired(); - - b.Property("CreationTime"); - - b.Property("EventTypeName") - .IsRequired(); - - b.Property("State"); - - b.Property("TimesSent"); - - b.HasKey("EventId"); - - b.ToTable("IntegrationEventLog"); - }); - } - } -} diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index b3fa6797e..bcceda43b 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -18,10 +18,6 @@ - - - - @@ -33,7 +29,11 @@ - + + + + + diff --git a/src/Services/Ordering/Ordering.API/Program.cs b/src/Services/Ordering/Ordering.API/Program.cs index 8fc18c8bb..9dba09bf9 100644 --- a/src/Services/Ordering/Ordering.API/Program.cs +++ b/src/Services/Ordering/Ordering.API/Program.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; using Microsoft.Extensions.Configuration; @@ -39,8 +38,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API new OrderingContextSeed() .SeedAsync(context, env, settings, logger) .Wait(); - }) - .MigrateDbContext((_, __) => { }); + }); Log.Information("Starting web host ({ApplicationContext})...", AppName); host.Run(); diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index cd34f98dd..aa4bc3ba1 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -1,10 +1,11 @@ -namespace Microsoft.eShopOnContainers.Services.Ordering.API +using Ordering.API.Application.IntegrationEvents.EventHandling; + +namespace Microsoft.eShopOnContainers.Services.Ordering.API { using AspNetCore.Http; using Autofac; using Autofac.Extensions.DependencyInjection; using global::Ordering.API.Application.IntegrationEvents; - using global::Ordering.API.Application.IntegrationEvents.Events; using global::Ordering.API.Infrastructure.Filters; using global::Ordering.API.Infrastructure.Middlewares; using Infrastructure.AutofacModules; @@ -14,16 +15,8 @@ using Microsoft.ApplicationInsights.ServiceFabric; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; - using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; - using Microsoft.Azure.ServiceBus; using Microsoft.EntityFrameworkCore; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; - using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; - using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -32,7 +25,6 @@ using Swashbuckle.AspNetCore.Swagger; using System; using System.Collections.Generic; - using System.Data.Common; using System.IdentityModel.Tokens.Jwt; using System.Reflection; using HealthChecks.UI.Client; @@ -57,6 +49,7 @@ .AddCustomSwagger(Configuration) .AddCustomIntegrations(Configuration) .AddCustomConfiguration(Configuration) + .AddIntegrationEventHandler() .AddEventBus(Configuration) .AddCustomAuthentication(Configuration); @@ -107,22 +100,7 @@ c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Ordering.API V1"); c.OAuthClientId("orderingswaggerui"); c.OAuthAppName("Ordering Swagger UI"); - }); - - ConfigureEventBus(app); - } - - - private void ConfigureEventBus(IApplicationBuilder app) - { - var eventBus = app.ApplicationServices.GetRequiredService(); - - eventBus.Subscribe>(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); + }); } protected virtual void ConfigureAuth(IApplicationBuilder app) @@ -229,18 +207,6 @@ }, ServiceLifetime.Scoped //Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request) ); - - services.AddDbContext(options => - { - options.UseSqlServer(configuration["ConnectionString"], - sqlServerOptionsAction: sqlOptions => - { - sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); - //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency - sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); - }); - }); - return services; } @@ -275,58 +241,25 @@ return services; } + public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + return services; + } + public static IServiceCollection AddCustomIntegrations(this IServiceCollection services, IConfiguration configuration) { services.AddSingleton(); - services.AddTransient(); - services.AddTransient>( - sp => (DbConnection c) => new IntegrationEventLogService(c)); + services.AddTransient(); services.AddTransient(); - - if (configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - - var factory = new ConnectionFactory() - { - HostName = configuration["EventBusConnection"] - }; - - if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) - { - factory.UserName = configuration["EventBusUserName"]; - } - - if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) - { - factory.Password = configuration["EventBusPassword"]; - } - - var retryCount = 5; - if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + return services; } @@ -358,41 +291,40 @@ public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) { - var subscriptionClientName = configuration["SubscriptionClientName"]; - - if (configuration.GetValue("AzureServiceBusEnabled")) + services.AddCap(options => { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + options.UseSqlServer(configuration["ConnectionString"]); - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else - { - services.AddSingleton(sp => + if (configuration.GetValue("AzureServiceBusEnabled")) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) + options.UseAzureServiceBus(configuration["EventBusConnection"]); + } + else + { + options.UseRabbitMQ(conf => { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); - }); - } + conf.HostName = configuration["EventBusConnection"]; + if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) + { + conf.UserName = configuration["EventBusUserName"]; + } + if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) + { + conf.Password = configuration["EventBusPassword"]; + } + }); + } + + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) + { + options.FailedRetryCount = int.Parse(configuration["EventBusRetryCount"]); + } - services.AddSingleton(); + if (!string.IsNullOrEmpty(configuration["SubscriptionClientName"])) + { + options.DefaultGroup = configuration["SubscriptionClientName"]; + } + }); return services; } diff --git a/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarioBase.cs b/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarioBase.cs index 5b2979e1a..d22a81d9c 100644 --- a/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarioBase.cs +++ b/src/Services/Ordering/Ordering.FunctionalTests/OrderingScenarioBase.cs @@ -12,7 +12,6 @@ using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.Extensions.Options; using Microsoft.Extensions.Logging; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; namespace Ordering.FunctionalTests { @@ -43,8 +42,7 @@ namespace Ordering.FunctionalTests new OrderingContextSeed() .SeedAsync(context, env, settings, logger) .Wait(); - }) - .MigrateDbContext((_, __) => { }); + }); return testServer; } diff --git a/test/ServicesTests/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs b/test/ServicesTests/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs index 3ce419230..0a27ec2fd 100644 --- a/test/ServicesTests/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs +++ b/test/ServicesTests/Application.FunctionalTests/Services/Catalog/CatalogScenariosBase.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.Services.Catalog.API; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.Extensions.DependencyInjection; @@ -40,8 +39,7 @@ namespace FunctionalTests.Services.Catalog new CatalogContextSeed() .SeedAsync(context, env, settings, logger) .Wait(); - }) - .MigrateDbContext((_, __) => { }); + }); return testServer; } diff --git a/test/ServicesTests/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs b/test/ServicesTests/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs index ad5230fcd..197f4a7f3 100644 --- a/test/ServicesTests/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs +++ b/test/ServicesTests/Application.FunctionalTests/Services/Ordering/OrderingScenariosBase.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.Services.Ordering.API; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; @@ -40,8 +39,7 @@ namespace FunctionalTests.Services.Ordering new OrderingContextSeed() .SeedAsync(context, env, settings, logger) .Wait(); - }) - .MigrateDbContext((_, __) => { }); + }); return testServer; }