From 5914c9d606c5db33df5b86eeee558c8603b84146 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 14 Mar 2019 14:40:32 +0800 Subject: [PATCH] Refactor Ordering SignalrHub eventbus using CAP --- .../GracePeriodConfirmedIntegrationEvent.cs | 6 +- .../Ordering.BackgroundTasks.csproj | 15 +- .../Ordering.BackgroundTasks/Startup.cs | 119 ++++----------- .../Tasks/GracePeriodManagerTask.cs | 10 +- ...aitingValidationIntegrationEventHandler.cs | 14 +- ...angedToCancelledIntegrationEventHandler.cs | 10 +- ...tusChangedToPaidIntegrationEventHandler.cs | 6 +- ...ChangedToShippedIntegrationEventHandler.cs | 8 +- ...ToStockConfirmedIntegrationEventHandler.cs | 13 +- ...angedToSubmittedIntegrationEventHandler.cs | 13 +- ...gedToAwaitingValidationIntegrationEvent.cs | 7 +- ...tatusChangedToCancelledIntegrationEvent.cs | 10 +- ...rderStatusChangedToPaidIntegrationEvent.cs | 10 +- ...rStatusChangedToShippedIntegrationEvent.cs | 10 +- ...ChangedToStockConfirmedIntegrationEvent.cs | 6 +- ...tatusChangedToSubmittedIntegrationEvent.cs | 10 +- .../Ordering.SignalrHub.csproj | 18 +-- .../Ordering/Ordering.SignalrHub/Startup.cs | 142 +++++------------- 18 files changed, 126 insertions(+), 301 deletions(-) diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/IntegrationEvents/GracePeriodConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.BackgroundTasks/IntegrationEvents/GracePeriodConfirmedIntegrationEvent.cs index df008ad90..0b8331237 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/IntegrationEvents/GracePeriodConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.BackgroundTasks/IntegrationEvents/GracePeriodConfirmedIntegrationEvent.cs @@ -1,8 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Ordering.BackgroundTasks.IntegrationEvents +namespace Ordering.BackgroundTasks.IntegrationEvents { - public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent + public class GracePeriodConfirmedIntegrationEvent { public int OrderId { get; } diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj b/src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj index f0fc00b04..a45943289 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj +++ b/src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj @@ -18,7 +18,11 @@ - + + + + + @@ -28,12 +32,5 @@ - - - - - - - - + diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs b/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs index 683fe1d0c..76bb079b6 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs +++ b/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs @@ -1,19 +1,11 @@ using Autofac; using Autofac.Extensions.DependencyInjection; 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.Hosting; -using Microsoft.Extensions.Logging; using Ordering.BackgroundTasks.Configuration; using Ordering.BackgroundTasks.Tasks; -using RabbitMQ.Client; using System; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; @@ -46,58 +38,43 @@ namespace Ordering.BackgroundTasks services.AddSingleton(); //configure event bus related services - - if (Configuration.GetValue("AzureServiceBusEnabled")) + 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"])) + options.UseRabbitMQ(conf => { - 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); - }); - } + 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"]; + } + }); //create autofac based service provider var container = new ContainerBuilder(); container.Populate(services); - - return new AutofacServiceProvider(container.Build()); } @@ -116,46 +93,6 @@ namespace Ordering.BackgroundTasks Predicate = r => r.Name.Contains("self") }); } - - - 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.AddSingleton(); - } } public static class CustomExtensionMethods diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs b/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs index 328fb95c4..e29886c8f 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs +++ b/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs @@ -1,5 +1,4 @@ using Dapper; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -10,6 +9,7 @@ using System.Collections.Generic; using System.Data.SqlClient; using System.Threading; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.BackgroundTasks.Tasks { @@ -18,11 +18,11 @@ namespace Ordering.BackgroundTasks.Tasks { private readonly ILogger _logger; private readonly BackgroundTaskSettings _settings; - private readonly IEventBus _eventBus; + private readonly ICapPublisher _eventBus; public GracePeriodManagerService( IOptions settings, - IEventBus eventBus, + ICapPublisher eventBus, ILogger logger) { _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); @@ -61,9 +61,9 @@ namespace Ordering.BackgroundTasks.Tasks { var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent(orderId); - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", confirmGracePeriodEvent.Id, Program.AppName, confirmGracePeriodEvent); + _logger.LogInformation("----- Publishing integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, confirmGracePeriodEvent); - _eventBus.Publish(confirmGracePeriodEvent); + _eventBus.Publish(nameof(GracePeriodConfirmedIntegrationEvent), confirmGracePeriodEvent); } } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs index 0e2665232..2adfc3814 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs @@ -1,15 +1,13 @@ using Microsoft.AspNetCore.SignalR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Logging; using Serilog.Context; -using System; -using System.Collections.Generic; -using System.Linq; +using System; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.SignalrHub.IntegrationEvents { - public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler + public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : ICapSubscribe { private readonly IHubContext _hubContext; private readonly ILogger _logger; @@ -22,12 +20,12 @@ namespace Ordering.SignalrHub.IntegrationEvents _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - + //TODO: [CapSubscribe(nameof(OrderStatusChangedToAwaitingValidationIntegrationEvent))] public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @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); await _hubContext.Clients .Group(@event.BuyerName) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs index 6257bb237..db70566c3 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs @@ -1,16 +1,14 @@ using Microsoft.AspNetCore.SignalR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; using Serilog.Context; -using System; -using System.Collections.Generic; -using System.Linq; +using System; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.SignalrHub.IntegrationEvents.EventHandling { - public class OrderStatusChangedToCancelledIntegrationEventHandler : IIntegrationEventHandler + public class OrderStatusChangedToCancelledIntegrationEventHandler : ICapSubscribe { private readonly IHubContext _hubContext; private readonly ILogger _logger; @@ -23,7 +21,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - + //TODO: [CapSubscribe(nameof(OrderStatusChangedToCancelledIntegrationEvent))] public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) { using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs index 836e02d3c..94df598ef 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs @@ -1,14 +1,14 @@ using Microsoft.AspNetCore.SignalR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; using Serilog.Context; using System; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.SignalrHub.IntegrationEvents.EventHandling { - public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler + public class OrderStatusChangedToPaidIntegrationEventHandler : ICapSubscribe { private readonly IHubContext _hubContext; private readonly ILogger _logger; @@ -21,7 +21,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - + //TODO [CapSubscribe(nameof(OrderStatusChangedToPaidIntegrationEvent))] public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) { using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs index 5b08d08b4..d92e31bbd 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs @@ -1,16 +1,14 @@ using Microsoft.AspNetCore.SignalR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; using Serilog.Context; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.SignalrHub.IntegrationEvents.EventHandling { - public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler + public class OrderStatusChangedToShippedIntegrationEventHandler : ICapSubscribe { private readonly IHubContext _hubContext; private readonly ILogger _logger; @@ -23,7 +21,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - + //TODO [CapSubscribe(nameof(OrderStatusChangedToShippedIntegrationEvent))] public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) { using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs index 348627716..1ed3c4add 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs @@ -1,17 +1,14 @@ using Microsoft.AspNetCore.SignalR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; using Serilog.Context; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.SignalrHub.IntegrationEvents.EventHandling { - public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : - IIntegrationEventHandler + public class OrderStatusChangedToStockConfirmedIntegrationEventHandler :ICapSubscribe { private readonly IHubContext _hubContext; private readonly ILogger _logger; @@ -24,12 +21,12 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling _logger = logger ?? throw new 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); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); await _hubContext.Clients .Group(@event.BuyerName) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs index 422bc4a7a..6ca4bcad1 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs @@ -1,17 +1,14 @@ using Microsoft.AspNetCore.SignalR; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.Extensions.Logging; using Ordering.SignalrHub.IntegrationEvents.Events; using Serilog.Context; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; +using DotNetCore.CAP; namespace Ordering.SignalrHub.IntegrationEvents.EventHandling { - public class OrderStatusChangedToSubmittedIntegrationEventHandler : - IIntegrationEventHandler + public class OrderStatusChangedToSubmittedIntegrationEventHandler :ICapSubscribe { private readonly IHubContext _hubContext; private readonly ILogger _logger; @@ -24,12 +21,12 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - + //TODO [CapSubscribe(nameof(OrderStatusChangedToSubmittedIntegrationEvent))] public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @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); await _hubContext.Clients .Group(@event.BuyerName) diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs index 2c5ecee27..58e04df8c 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs @@ -1,9 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System.Collections.Generic; - -namespace Ordering.SignalrHub.IntegrationEvents +namespace Ordering.SignalrHub.IntegrationEvents { - public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToAwaitingValidationIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs index e9ac5b39b..a015c171d 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/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.SignalrHub.IntegrationEvents.Events +namespace Ordering.SignalrHub.IntegrationEvents.Events { - public class OrderStatusChangedToCancelledIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToCancelledIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs index beb49965d..ca1584226 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.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.SignalrHub.IntegrationEvents.Events +namespace Ordering.SignalrHub.IntegrationEvents.Events { - public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToPaidIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs index 0768c7f4e..a72ea9ae9 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/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.SignalrHub.IntegrationEvents.Events +namespace Ordering.SignalrHub.IntegrationEvents.Events { - public class OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToShippedIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs index 588505c8f..b5c07543e 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs @@ -1,8 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Ordering.SignalrHub.IntegrationEvents.Events +namespace Ordering.SignalrHub.IntegrationEvents.Events { - public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToStockConfirmedIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToSubmittedIntegrationEvent.cs b/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToSubmittedIntegrationEvent.cs index 5ea0cec71..3d397118d 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToSubmittedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/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.SignalrHub.IntegrationEvents.Events +namespace Ordering.SignalrHub.IntegrationEvents.Events { - public class OrderStatusChangedToSubmittedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToSubmittedIntegrationEvent { public int OrderId { get; } public string OrderStatus { get; } diff --git a/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj b/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj index a4dd8cc9d..752b40bfc 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj +++ b/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj @@ -4,16 +4,16 @@ netcoreapp2.2 ..\..\..\..\docker-compose.dcproj - - - - - + + + + + @@ -30,12 +30,6 @@ - - - - - - - + diff --git a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs index e8bb7ecd1..0b0c34ebb 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs @@ -2,19 +2,12 @@ using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; -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 Ordering.SignalrHub.AutofacModules; using Ordering.SignalrHub.IntegrationEvents; using Ordering.SignalrHub.IntegrationEvents.EventHandling; -using Ordering.SignalrHub.IntegrationEvents.Events; -using RabbitMQ.Client; using System; using System.IdentityModel.Tokens.Jwt; using HealthChecks.UI.Client; @@ -59,54 +52,43 @@ namespace Ordering.SignalrHub services.AddSignalR(); } - 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"])) + options.UseRabbitMQ(conf => { - factory.Password = Configuration["EventBusPassword"]; - } - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } + 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"]); + } - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"])) + { + options.DefaultGroup = Configuration["SubscriptionClientName"]; + } + }); ConfigureAuthService(services); - RegisterEventBus(services); - services.AddOptions(); //configure autofac @@ -151,21 +133,7 @@ namespace Ordering.SignalrHub { routes.MapHub("/notificationhub", options => options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransports.All); - }); - - ConfigureEventBus(app); - } - - private void ConfigureEventBus(IApplicationBuilder app) - { - var eventBus = app.ApplicationServices.GetRequiredService(); - - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); + }); } private void ConfigureAuthService(IServiceCollection services) @@ -187,49 +155,21 @@ namespace Ordering.SignalrHub options.Audience = "orders.signalrhub"; }); } - - 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.AddSingleton(); - } } public static class CustomExtensionMethods { + public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services) + { + services.AddTransient(); //Subscribe for OrderStatusChangedToAwaitingValidationIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToPaidIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToStockConfirmedIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToShippedIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToCancelledIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToSubmittedIntegrationEvent + return services; + } + public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) { var hcBuilder = services.AddHealthChecks();