diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs index b26d64d40..7d2e3e2b7 100644 --- a/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs @@ -1,22 +1,21 @@ -namespace Payment.API.IntegrationEvents.EventHandling +using DotNetCore.CAP; + +namespace Payment.API.IntegrationEvents.EventHandling { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Payment.API.IntegrationEvents.Events; using Serilog.Context; using System.Threading.Tasks; - public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : - IIntegrationEventHandler + public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : ICapSubscribe { - private readonly IEventBus _eventBus; + private readonly ICapPublisher _eventBus; private readonly PaymentSettings _settings; private readonly ILogger _logger; public OrderStatusChangedToStockConfirmedIntegrationEventHandler( - IEventBus eventBus, + ICapPublisher eventBus, IOptionsSnapshot settings, ILogger logger) { @@ -25,13 +24,12 @@ _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); } + //TODO: [CapSubscribe(nameof(OrderStatusChangedToStockConfirmedIntegrationEvent))] public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @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); - - IntegrationEvent orderPaymentIntegrationEvent; + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); //Business feature comment: // When OrderStatusChangedToStockConfirmed Integration Event is handled. @@ -41,18 +39,16 @@ if (_settings.PaymentSucceded) { - orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); + var orderPaymentSucceededIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); + await _eventBus.PublishAsync(nameof(OrderPaymentSuccededIntegrationEvent), orderPaymentSucceededIntegrationEvent); + _logger.LogInformation("----- Publishing integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, orderPaymentSucceededIntegrationEvent); } else { - orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId); + var orderPaymentFailedIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId); + await _eventBus.PublishAsync(nameof(OrderPaymentFailedIntegrationEvent), orderPaymentFailedIntegrationEvent); + _logger.LogInformation("----- Publishing integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, orderPaymentFailedIntegrationEvent); } - - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", orderPaymentIntegrationEvent.Id, Program.AppName, orderPaymentIntegrationEvent); - - _eventBus.Publish(orderPaymentIntegrationEvent); - - await Task.CompletedTask; } } } diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs index d51c518c4..b663b5baf 100644 --- a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs +++ b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Payment.API.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderPaymentFailedIntegrationEvent : IntegrationEvent + public class OrderPaymentFailedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs index d672ff9d4..f3a039606 100644 --- a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs +++ b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Payment.API.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent + public class OrderPaymentSuccededIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs index 88f60e0d6..33964d122 100644 --- a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs +++ b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs @@ -1,8 +1,6 @@ namespace Payment.API.IntegrationEvents.Events { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToStockConfirmedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Payment/Payment.API/Payment.API.csproj b/src/Services/Payment/Payment.API/Payment.API.csproj index e912727e8..b4b7ffd1f 100644 --- a/src/Services/Payment/Payment.API/Payment.API.csproj +++ b/src/Services/Payment/Payment.API/Payment.API.csproj @@ -11,6 +11,10 @@ + + + + @@ -25,11 +29,5 @@ - - - - - - diff --git a/src/Services/Payment/Payment.API/Startup.cs b/src/Services/Payment/Payment.API/Startup.cs index 4d5010868..36d82411b 100644 --- a/src/Services/Payment/Payment.API/Startup.cs +++ b/src/Services/Payment/Payment.API/Startup.cs @@ -4,17 +4,10 @@ using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.ServiceFabric; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Azure.ServiceBus; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Payment.API.IntegrationEvents.EventHandling; -using Payment.API.IntegrationEvents.Events; -using RabbitMQ.Client; using System; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; @@ -39,49 +32,40 @@ namespace Payment.API RegisterAppInsights(services); - if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddIntegrationEventHandler(); + services.AddCap(options => { - services.AddSingleton(sp => + options.UseInMemoryStorage(); + if (Configuration.GetValue("AzureServiceBusEnabled")) { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => + options.UseAzureServiceBus(Configuration["EventBusConnection"]); + } + else { - 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"])) + options.UseRabbitMQ(conf => { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, 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"]); + } - RegisterEventBus(services); + if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"])) + { + options.DefaultGroup = Configuration["SubscriptionClientName"]; + } + }); var container = new ContainerBuilder(); container.Populate(services); @@ -110,9 +94,7 @@ namespace Payment.API app.UseHealthChecks("/liveness", new HealthCheckOptions { Predicate = r => r.Name.Contains("self") - }); - - ConfigureEventBus(app); + }); } private void RegisterAppInsights(IServiceCollection services) @@ -132,56 +114,16 @@ namespace Payment.API new FabricTelemetryInitializer()); } } - - private void RegisterEventBus(IServiceCollection services) - { - var subscriptionClientName = Configuration["SubscriptionClientName"]; - - if (Configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else - { - services.AddSingleton(sp => - { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); - }); - } - - services.AddTransient(); - services.AddSingleton(); - } - - private void ConfigureEventBus(IApplicationBuilder app) - { - var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe(); - } } public static class CustomExtensionMethods { + public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services) + { + services.AddTransient(); //Subscribe for OrderStatusChangedToStockConfirmedIntegrationEvent + return services; + } + public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) { var hcBuilder = services.AddHealthChecks();