From 5ddf84d1b63e977f93f6d1ef1ddfcbdc871e2f23 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 21 Apr 2020 07:30:41 +0200 Subject: [PATCH 01/37] Move into correct namespace --- Microservice.Api/Startup.cs | 2 +- .../BackgroundJobServerServiceCollectionExtensions.cs | 10 ++++------ .../Configuration/BackgroundJobServerSettings.cs | 2 +- .../IRecurringJob.cs | 6 +++--- .../Infrastructure/BackgroundProcessActivator.cs | 2 +- .../Infrastructure/BackgroundProcessingClient.cs | 8 ++++---- .../Infrastructure/IBackgroundProcessingClient.cs | 2 +- 7 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 38da4eb..c06b5d8 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -2,7 +2,7 @@ using MediatR; using Microservice.Api.Filters; using Microservice.Db.Configuration; -using Microservice.HanfireWithRedisBackingStore.Configuration; +using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; diff --git a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs index 42d2629..fa2ac85 100644 --- a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs +++ b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs @@ -1,16 +1,14 @@ -using Hangfire; +using System.Collections.Generic; +using Hangfire; using Hangfire.Dashboard; using Hangfire.PostgreSql; -using Microservice.HanfireWithRedisBackingStore.Infrastructure; +using Microservice.HangfireBackgroundJobServer.Infrastructure; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -namespace Microservice.HanfireWithRedisBackingStore.Configuration +namespace Microservice.HangfireBackgroundJobServer.Configuration { public static class BackgroundJobServerServiceCollectionExtensions { diff --git a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerSettings.cs b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerSettings.cs index 9252c3b..d50b2c9 100644 --- a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerSettings.cs +++ b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerSettings.cs @@ -1,4 +1,4 @@ -namespace Microservice.HanfireWithRedisBackingStore.Configuration +namespace Microservice.HangfireBackgroundJobServer.Configuration { public class BackgroundJobServerSettings { diff --git a/Microservice.HangfireBackgroundJobServer/IRecurringJob.cs b/Microservice.HangfireBackgroundJobServer/IRecurringJob.cs index 626f0d6..f735d87 100644 --- a/Microservice.HangfireBackgroundJobServer/IRecurringJob.cs +++ b/Microservice.HangfireBackgroundJobServer/IRecurringJob.cs @@ -1,7 +1,7 @@ -using Hangfire; -using System.Threading.Tasks; +using System.Threading.Tasks; +using Hangfire; -namespace Microservice.HanfireWithRedisBackingStore +namespace Microservice.HangfireBackgroundJobServer { public interface IRecurringJob { diff --git a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessActivator.cs b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessActivator.cs index bbdfad7..a6d3ab7 100644 --- a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessActivator.cs +++ b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessActivator.cs @@ -2,7 +2,7 @@ using Hangfire; using Microsoft.Extensions.DependencyInjection; -namespace Microservice.HanfireWithRedisBackingStore.Infrastructure +namespace Microservice.HangfireBackgroundJobServer.Infrastructure { public class BackgroundProcessActivator : JobActivator { diff --git a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs index 420b60d..d75624c 100644 --- a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs +++ b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs @@ -1,10 +1,10 @@ -using Hangfire; -using Microsoft.Extensions.DependencyInjection; -using System; +using System; using System.Linq.Expressions; using System.Threading.Tasks; +using Hangfire; +using Microsoft.Extensions.DependencyInjection; -namespace Microservice.HanfireWithRedisBackingStore.Infrastructure +namespace Microservice.HangfireBackgroundJobServer.Infrastructure { public class BackgroundProcessingClient : IBackgroundProcessingClient { diff --git a/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs b/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs index a522471..d39bcee 100644 --- a/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs +++ b/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs @@ -2,7 +2,7 @@ using System.Linq.Expressions; using System.Threading.Tasks; -namespace Microservice.HanfireWithRedisBackingStore.Infrastructure +namespace Microservice.HangfireBackgroundJobServer.Infrastructure { public interface IBackgroundProcessingClient { From e09daa880b1f92d1007ad2531302dfb3178994c4 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 21 Apr 2020 07:30:52 +0200 Subject: [PATCH 02/37] Update IRabbitMessageBrokerClient.cs --- .../IRabbitMessageBrokerClient.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Microservice.RabbitMessageBroker/IRabbitMessageBrokerClient.cs b/Microservice.RabbitMessageBroker/IRabbitMessageBrokerClient.cs index 8fce1e1..f0c7133 100644 --- a/Microservice.RabbitMessageBroker/IRabbitMessageBrokerClient.cs +++ b/Microservice.RabbitMessageBroker/IRabbitMessageBrokerClient.cs @@ -7,17 +7,19 @@ namespace Microservice.RabbitMessageBroker public interface IRabbitMessageBrokerClient { Task Subscribe( - string topic, - string subscriptionId, + string topic, + string subscriptionId, Func onReceive, - Func configure = null); + Func configure = null + ); Task Subscribe( string topic, string subscriptionId, Func onReceive, Type eventModelType, - Func config = null); + Func configure = null + ); Task Publish(string topic, T message); From 3e662ec2a2626c960f82aa79bf2060d66bce88db Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 21 Apr 2020 07:31:22 +0200 Subject: [PATCH 03/37] Add Messagebus subscribe helpers --- .../Subscriptions/OrderPlacedSubscription.cs | 32 -------- Microservice.Api/Microservice.Api.csproj | 4 + Microservice.Logic/Microservice.Logic.csproj | 2 +- .../Orders/Handlers/CreateOrderHandler.cs | 10 ++- .../IOrderCreatedEventPublisher.cs | 6 -- Microservice.Logic/Orders/OrderMapper.cs | 13 ++++ .../IMessageBusPublisher.cs | 9 +++ .../Microservice.MessageBus.csproj | 15 ++++ .../Orders/Events/OrderCreatedEvent.cs | 2 +- .../Events/OrderPlacedSubscriptionEvent.cs | 2 +- .../Publishers}/OrderCreatedEventPublisher.cs | 10 +-- .../Publishers/OrderUpdatedEventPublisher.cs | 20 +++++ .../Subscribers/OrderPlacedSubscription.cs | 44 +++++++++++ ...undJobHelpersServiceCollectionExtension.cs | 10 +++ ...itMessageBrokerBackgroundJobHelpers.csproj | 16 ++++ .../MessageBrokerSubscriptionBackgroundJob.cs | 74 +++++++++++++++++++ ...BrokerSubscriptionsConfigurationBuilder.cs | 37 ++++++++++ .../ServiceCollectionExtensions.cs | 15 ++++ .../IMessageBrokerSubscriptionHandler .cs | 9 +++ .../MessageBrokerSubscriptionHandler.cs | 14 ++++ ...oservice.RabbitMessageBrokerHelpers.csproj | 16 ++++ .../Models/MessageBrokerSubscription.cs | 14 ++++ Microservice.sln | 12 +++ 23 files changed, 337 insertions(+), 49 deletions(-) delete mode 100644 Microservice.Api/MessageBus/OrderSubscriptions/Subscriptions/OrderPlacedSubscription.cs delete mode 100644 Microservice.Logic/Orders/Integration/IOrderCreatedEventPublisher.cs create mode 100644 Microservice.MessageBus/IMessageBusPublisher.cs create mode 100644 Microservice.MessageBus/Microservice.MessageBus.csproj rename {Microservice.Logic => Microservice.MessageBus}/Orders/Events/OrderCreatedEvent.cs (71%) rename {Microservice.Api/MessageBus/OrderSubscriptions => Microservice.MessageBus/Orders}/Events/OrderPlacedSubscriptionEvent.cs (60%) rename {Microservice.Logic/Orders/Integration => Microservice.MessageBus/Orders/Publishers}/OrderCreatedEventPublisher.cs (57%) create mode 100644 Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs create mode 100644 Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs create mode 100644 Microservice.RabbitMessageBrokerBackgroundJobHelpers/Configuration/MessageBrokerBackgroundJobHelpersServiceCollectionExtension.cs create mode 100644 Microservice.RabbitMessageBrokerBackgroundJobHelpers/Microservice.RabbitMessageBrokerBackgroundJobHelpers.csproj create mode 100644 Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj create mode 100644 Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs diff --git a/Microservice.Api/MessageBus/OrderSubscriptions/Subscriptions/OrderPlacedSubscription.cs b/Microservice.Api/MessageBus/OrderSubscriptions/Subscriptions/OrderPlacedSubscription.cs deleted file mode 100644 index f9297ca..0000000 --- a/Microservice.Api/MessageBus/OrderSubscriptions/Subscriptions/OrderPlacedSubscription.cs +++ /dev/null @@ -1,32 +0,0 @@ -using MediatR; -using Microservice.RabbitMessageBroker; -using Microsoft.Extensions.Hosting; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Microservice.Api.MessageBus.OrderSubscriptions.Subscriptions -{ - public class OrderPlacedSubscription: IHostedService - { - private readonly IRabbitMessageBrokerClient _messageBrokerClient; - private readonly IServiceProvider _serviceProvider; - private readonly IMediator _mediator; - public OrderPlacedSubscription(IRabbitMessageBrokerClient messageBrokerClient, IServiceProvider serviceProvider, IMediator mediator) - { - _messageBrokerClient = messageBrokerClient; - _serviceProvider = serviceProvider; - _mediator = mediator; - } - - public Task StartAsync(CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - } -} diff --git a/Microservice.Api/Microservice.Api.csproj b/Microservice.Api/Microservice.Api.csproj index cfff474..8fc63a0 100644 --- a/Microservice.Api/Microservice.Api.csproj +++ b/Microservice.Api/Microservice.Api.csproj @@ -26,4 +26,8 @@ + + + + diff --git a/Microservice.Logic/Microservice.Logic.csproj b/Microservice.Logic/Microservice.Logic.csproj index e447d6a..dd893da 100644 --- a/Microservice.Logic/Microservice.Logic.csproj +++ b/Microservice.Logic/Microservice.Logic.csproj @@ -12,6 +12,6 @@ - + diff --git a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs index 3d2d054..6d9b160 100644 --- a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs @@ -3,18 +3,22 @@ using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Commands; using Microservice.Logic.Orders.Responses; +using Microservice.MessageBus; +using Microservice.MessageBus.Orders.Publishers; using System.Threading; using System.Threading.Tasks; namespace Microservice.Logic.Orders.Handlers { - public class CreateOrderHandler: IRequestHandler + public class CreateOrderHandler : IRequestHandler { private readonly MicroserviceDbContext _dbContext; + private readonly IMessageBusPublisher _createdEventPublisher; - public CreateOrderHandler(MicroserviceDbContext dbContext) + public CreateOrderHandler(MicroserviceDbContext dbContext, IMessageBusPublisher createdEventPublisher) { _dbContext = dbContext; + _createdEventPublisher = createdEventPublisher; } public async Task Handle(CreateOrderCommand request, CancellationToken cancellationToken) @@ -29,6 +33,8 @@ public async Task Handle(CreateOrderCommand request, Cancellation var response = entity.ToResponse(); + await _createdEventPublisher.Publish(entity.ToCreatedEvent()); + return response; } } diff --git a/Microservice.Logic/Orders/Integration/IOrderCreatedEventPublisher.cs b/Microservice.Logic/Orders/Integration/IOrderCreatedEventPublisher.cs deleted file mode 100644 index 888c032..0000000 --- a/Microservice.Logic/Orders/Integration/IOrderCreatedEventPublisher.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Microservice.Logic.Orders.Integration -{ - public interface IOrderCreatedEventPublisher - { - } -} diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index f3c8fe7..9b401fa 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -1,6 +1,7 @@ using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Models; using Microservice.Logic.Orders.Responses; +using Microservice.MessageBus.Orders.Events; namespace Microservice.Logic.Orders { @@ -26,5 +27,17 @@ public static OrderPatchModel ToPatchModal(this Order order) return result; } + + public static OrderCreatedEvent ToCreatedEvent(this Order order) + { + var result = new OrderCreatedEvent + { + Name = order.Name, + Id = order.Id + }; + + return result; + + } } } diff --git a/Microservice.MessageBus/IMessageBusPublisher.cs b/Microservice.MessageBus/IMessageBusPublisher.cs new file mode 100644 index 0000000..a68e845 --- /dev/null +++ b/Microservice.MessageBus/IMessageBusPublisher.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Microservice.MessageBus +{ + public interface IMessageBusPublisher + { + Task Publish(TMessage message); + } +} diff --git a/Microservice.MessageBus/Microservice.MessageBus.csproj b/Microservice.MessageBus/Microservice.MessageBus.csproj new file mode 100644 index 0000000..6383d06 --- /dev/null +++ b/Microservice.MessageBus/Microservice.MessageBus.csproj @@ -0,0 +1,15 @@ + + + + netcoreapp3.1 + + + + + + + + + + + diff --git a/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs b/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs similarity index 71% rename from Microservice.Logic/Orders/Events/OrderCreatedEvent.cs rename to Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs index 210bd6f..45bfb14 100644 --- a/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs +++ b/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs @@ -1,4 +1,4 @@ -namespace Microservice.Logic.Orders.Events +namespace Microservice.MessageBus.Orders.Events { public class OrderCreatedEvent { diff --git a/Microservice.Api/MessageBus/OrderSubscriptions/Events/OrderPlacedSubscriptionEvent.cs b/Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs similarity index 60% rename from Microservice.Api/MessageBus/OrderSubscriptions/Events/OrderPlacedSubscriptionEvent.cs rename to Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs index 2670e25..36ccfe3 100644 --- a/Microservice.Api/MessageBus/OrderSubscriptions/Events/OrderPlacedSubscriptionEvent.cs +++ b/Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs @@ -1,4 +1,4 @@ -namespace Microservice.Api.MessageBus.Orders.IntegrationEvents +namespace Microservice.MessageBus.Orders.Events { public class OrderPlacedSubscriptionEvent { diff --git a/Microservice.Logic/Orders/Integration/OrderCreatedEventPublisher.cs b/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs similarity index 57% rename from Microservice.Logic/Orders/Integration/OrderCreatedEventPublisher.cs rename to Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs index 4572eb9..c0531f6 100644 --- a/Microservice.Logic/Orders/Integration/OrderCreatedEventPublisher.cs +++ b/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs @@ -1,10 +1,9 @@ -using Microservice.Logic.Orders.Events; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microservice.RabbitMessageBroker; -namespace Microservice.Logic.Orders.Integration +namespace Microservice.MessageBus.Orders.Publishers { - public class OrderCreatedEventPublisher : IOrderCreatedEventPublisher + public class OrderCreatedEventPublisher: IMessageBusPublisher { private IRabbitMessageBrokerClient BrokerClient { get; } @@ -13,10 +12,9 @@ public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) BrokerClient = brokerClient; } - public async Task Publish(OrderCreatedEvent createdEvent) + public async Task Publish(TMessage createdEvent) { await BrokerClient.Publish("OrderCreated", createdEvent); - } } } diff --git a/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs b/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs new file mode 100644 index 0000000..16e5529 --- /dev/null +++ b/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using Microservice.RabbitMessageBroker; + +namespace Microservice.MessageBus.Orders.Publishers +{ + public class OrderUpdatedEventPublisher: IMessageBusPublisher + { + private IRabbitMessageBrokerClient BrokerClient { get; } + + public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) + { + BrokerClient = brokerClient; + } + + public async Task Publish(TMessage message) + { + await BrokerClient.Publish("OrderUpdated", message); + } + } +} diff --git a/Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs b/Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs new file mode 100644 index 0000000..6f7c3e2 --- /dev/null +++ b/Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs @@ -0,0 +1,44 @@ +using Microservice.MessageBus.Orders.Events; +using Microservice.RabbitMessageBroker; +using Microsoft.Extensions.Hosting; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Microservice.Api.MessageBus.OrderSubscriptions.Subscriptions +{ + public class OrderPlacedSubscription : IHostedService + { + private readonly IRabbitMessageBrokerClient _messageBrokerClient; + private readonly IServiceProvider _serviceProvider; + public OrderPlacedSubscription(IRabbitMessageBrokerClient messageBrokerClient, IServiceProvider serviceProvider) + { + _messageBrokerClient = messageBrokerClient; + _serviceProvider = serviceProvider; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + return _messageBrokerClient.Subscribe("OrderPlaced", "Orders", HandleMessage, c => c.UseBasicQos()); + } + + private async Task HandleMessage(OrderPlacedSubscriptionEvent orderPlacedSubscriptionEvent) + { + //using (var scope = _serviceProvider.CreateScope()) + //{ + // var serviceProvider = scope.ServiceProvider; + // var mergeSellerLeadsForPeopleManager = serviceProvider.GetService(); + // await _backgroundProcessingClient.Run( + // () => mergeSellerLeadsForPeopleManager.Merge( + // message.NewPersonId, + // message.OldPersonId, + // message.ActionedById)); + //} + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +} diff --git a/Microservice.RabbitMessageBrokerBackgroundJobHelpers/Configuration/MessageBrokerBackgroundJobHelpersServiceCollectionExtension.cs b/Microservice.RabbitMessageBrokerBackgroundJobHelpers/Configuration/MessageBrokerBackgroundJobHelpersServiceCollectionExtension.cs new file mode 100644 index 0000000..10e11f1 --- /dev/null +++ b/Microservice.RabbitMessageBrokerBackgroundJobHelpers/Configuration/MessageBrokerBackgroundJobHelpersServiceCollectionExtension.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microservice.RabbitMessageBrokerBackgroundJobHelpers.Configuration +{ + class MessageBrokerBackgroundJobHelpersServiceCollectionExtension + { + } +} diff --git a/Microservice.RabbitMessageBrokerBackgroundJobHelpers/Microservice.RabbitMessageBrokerBackgroundJobHelpers.csproj b/Microservice.RabbitMessageBrokerBackgroundJobHelpers/Microservice.RabbitMessageBrokerBackgroundJobHelpers.csproj new file mode 100644 index 0000000..dbf9da1 --- /dev/null +++ b/Microservice.RabbitMessageBrokerBackgroundJobHelpers/Microservice.RabbitMessageBrokerBackgroundJobHelpers.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + diff --git a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs new file mode 100644 index 0000000..222f17e --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs @@ -0,0 +1,74 @@ +using Microservice.RabbitMessageBroker; +using Microservice.RabbitMessageBrokerHelpers.Builders; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using Microservice.RabbitMessageBrokerHelpers.Models; +using Microsoft.Extensions.DependencyInjection; + +namespace Microservice.RabbitMessageBrokerHelpers.BackgroundJobs +{ + public class MessageBrokerSubscriptionBackgroundJob : IHostedService + { + private readonly IServiceProvider _serviceProvider; + private readonly IBackgroundProcessingClient _backgroundProcessingClient; + private readonly IRabbitMessageBrokerClient _messageBrokerClient; + private readonly ILogger _logger; + private readonly MessageBrokerSubscriptionsConfigurationBuilder _configurationBuilder; + private List _unsubscribeCallbacks; + + public MessageBrokerSubscriptionBackgroundJob(MessageBrokerSubscriptionsConfigurationBuilder configurationBuilder, IServiceProvider serviceProvider, IRabbitMessageBrokerClient messageBrokerClient, ILogger logger, List unsubscribeCallbacks) + { + _configurationBuilder = configurationBuilder; + _serviceProvider = serviceProvider; + _messageBrokerClient = messageBrokerClient; + _logger = logger; + _unsubscribeCallbacks = unsubscribeCallbacks; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + var tasks = _configurationBuilder.Subscriptions + .Select(subscription => _messageBrokerClient.Subscribe( + subscription.Topic, + subscription.Pool, + eventModel => Handle(eventModel, subscription), + subscription.EventModel)) + .ToList(); + + await Task.WhenAll(tasks); + + _unsubscribeCallbacks = tasks.Select(t => t.Result).ToList(); + } + + public async Task Handle(object eventModel, MessageBrokerSubscription subscription) + { + using (var scope = _serviceProvider.CreateScope()) + { + var handler = scope.ServiceProvider.GetService(subscription.Handler) as IMessageBrokerSubscriptionHandler; + if (handler == null) + { + _logger.LogError("Could not resolve subscription handler. {@details}", new + { + CouldNotResolveIntegrationEventHandler = new + { + subscription + } + }); + } + + await _backgroundProcessingClient.Run(() => handler.HandleEventModel(eventModel)); + } + } + + public Task StopAsync(CancellationToken cancellationToken) + { + throw new System.NotImplementedException(); + } + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs new file mode 100644 index 0000000..a5db714 --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs @@ -0,0 +1,37 @@ +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using Microservice.RabbitMessageBrokerHelpers.Models; +using System.Collections.Generic; + +namespace Microservice.RabbitMessageBrokerHelpers.Builders +{ + public class MessageBrokerSubscriptionsConfigurationBuilder + { + public List Subscriptions; + public string Pool; + + public MessageBrokerSubscriptionsConfigurationBuilder UsePool(string pool) + { + Pool = pool; + return this; + } + + public MessageBrokerSubscriptionsConfigurationBuilder Subscribe(string topic) + where THandler : MessageBrokerSubscriptionHandler + where TEventModel : class + { + Subscriptions.Add(new MessageBrokerSubscription + { + Topic = topic, + Handler = typeof(THandler), + EventModel = typeof(TEventModel) + }); + return this; + } + + public MessageBrokerSubscriptionsConfigurationBuilder Subscribe(string topic) + where THandler : MessageBrokerSubscriptionHandler + { + return Subscribe(topic); + } + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs b/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..c5080a1 --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs @@ -0,0 +1,15 @@ +using Microservice.RabbitMessageBrokerHelpers.Builders; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Microservice.RabbitMessageBrokerHelpers.Configuration +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddMessageBrokerSubscriptions(this IServiceCollection services, + Action configure) + { + return services; + } + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs new file mode 100644 index 0000000..511674f --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Microservice.RabbitMessageBrokerHelpers.Handlers +{ + public interface IMessageBrokerSubscriptionHandler + { + Task HandleEventModel(object eventModel); + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs new file mode 100644 index 0000000..a101e60 --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs @@ -0,0 +1,14 @@ +using System.Threading.Tasks; + +namespace Microservice.RabbitMessageBrokerHelpers.Handlers +{ + public abstract class MessageBrokerSubscriptionHandler : IMessageBrokerSubscriptionHandler where TEventModel : class + { + public Task HandleEventModel(object eventModel) + { + return Handle(eventModel as TEventModel); + } + + public abstract Task Handle(TEventModel eventModel); + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj b/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj new file mode 100644 index 0000000..eadb8f3 --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + diff --git a/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs b/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs new file mode 100644 index 0000000..c0e13a5 --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microservice.RabbitMessageBrokerHelpers.Models +{ + public class MessageBrokerSubscription + { + public string Pool { get; set; } + public Type Handler { get; set; } + public Type EventModel { get; set; } + public string Topic { get; set; } + } +} diff --git a/Microservice.sln b/Microservice.sln index 2d755af..0b6cb18 100644 --- a/Microservice.sln +++ b/Microservice.sln @@ -17,6 +17,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.RabbitMessageB EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.HangfireBackgroundJobServer", "Microservice.HangfireBackgroundJobServer\Microservice.HangfireBackgroundJobServer.csproj", "{96C85DBD-DE58-4D02-A01B-E10D521CB089}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microservice.MessageBus", "Microservice.MessageBus\Microservice.MessageBus.csproj", "{5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microservice.RabbitMessageBrokerHelpers", "Microservice.RabbitMessageBrokerHelpers\Microservice.RabbitMessageBrokerHelpers.csproj", "{CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +55,14 @@ Global {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Debug|Any CPU.Build.0 = Debug|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Release|Any CPU.ActiveCfg = Release|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Release|Any CPU.Build.0 = Release|Any CPU + {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Release|Any CPU.Build.0 = Release|Any CPU + {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From ba454c601da497593430b838e8b9f1ed94f7377b Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 21 Apr 2020 07:35:42 +0200 Subject: [PATCH 04/37] Update MessageBrokerSubscriptionBackgroundJob.cs --- .../MessageBrokerSubscriptionBackgroundJob.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs index 222f17e..ff19011 100644 --- a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs +++ b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs @@ -1,5 +1,9 @@ -using Microservice.RabbitMessageBroker; +using Microservice.HangfireBackgroundJobServer.Infrastructure; +using Microservice.RabbitMessageBroker; using Microservice.RabbitMessageBrokerHelpers.Builders; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using Microservice.RabbitMessageBrokerHelpers.Models; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; @@ -7,9 +11,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microservice.RabbitMessageBrokerHelpers.Handlers; -using Microservice.RabbitMessageBrokerHelpers.Models; -using Microsoft.Extensions.DependencyInjection; namespace Microservice.RabbitMessageBrokerHelpers.BackgroundJobs { @@ -22,13 +23,14 @@ public class MessageBrokerSubscriptionBackgroundJob : IHostedService private readonly MessageBrokerSubscriptionsConfigurationBuilder _configurationBuilder; private List _unsubscribeCallbacks; - public MessageBrokerSubscriptionBackgroundJob(MessageBrokerSubscriptionsConfigurationBuilder configurationBuilder, IServiceProvider serviceProvider, IRabbitMessageBrokerClient messageBrokerClient, ILogger logger, List unsubscribeCallbacks) + public MessageBrokerSubscriptionBackgroundJob(MessageBrokerSubscriptionsConfigurationBuilder configurationBuilder, IServiceProvider serviceProvider, IRabbitMessageBrokerClient messageBrokerClient, ILogger logger, List unsubscribeCallbacks, IBackgroundProcessingClient backgroundProcessingClient) { _configurationBuilder = configurationBuilder; _serviceProvider = serviceProvider; _messageBrokerClient = messageBrokerClient; _logger = logger; _unsubscribeCallbacks = unsubscribeCallbacks; + _backgroundProcessingClient = backgroundProcessingClient; } public async Task StartAsync(CancellationToken cancellationToken) @@ -68,7 +70,12 @@ public async Task Handle(object eventModel, MessageBrokerSubscription subscripti public Task StopAsync(CancellationToken cancellationToken) { - throw new System.NotImplementedException(); + foreach (var unsubscribeCallback in _unsubscribeCallbacks) + { + unsubscribeCallback(); + } + + return Task.CompletedTask; } } } From a14f2d24c50b745c4e6b9efedccb846f917e32a1 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 21 Apr 2020 07:35:45 +0200 Subject: [PATCH 05/37] Update ServiceCollectionExtensions.cs --- .../Configuration/ServiceCollectionExtensions.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs b/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs index c5080a1..49761b8 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ -using Microservice.RabbitMessageBrokerHelpers.Builders; +using Microservice.RabbitMessageBrokerHelpers.BackgroundJobs; +using Microservice.RabbitMessageBrokerHelpers.Builders; using Microsoft.Extensions.DependencyInjection; using System; @@ -9,7 +10,16 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddMessageBrokerSubscriptions(this IServiceCollection services, Action configure) { - return services; + var configurationBuilder = new MessageBrokerSubscriptionsConfigurationBuilder(); + configure.Invoke(configurationBuilder); + + foreach (var subscription in configurationBuilder.Subscriptions) + services.AddTransient(subscription.Handler); + + return services + .AddSingleton(configurationBuilder) + .AddHostedService() + ; } } } From 07e0400c0c81a8e6c1f833e2089e3d59105fe9e1 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 25 Apr 2020 18:44:08 +0200 Subject: [PATCH 06/37] Work In Progress --- Microservice.Api/Microservice.Api.csproj | 1 + Microservice.Api/Startup.cs | 6 +++ Microservice.Logic/Microservice.Logic.csproj | 2 +- .../Events/OrderPlacedSubscriptionEvent.cs | 2 +- .../OrderPlacedSubscriptionHandler.cs | 20 +++++++++ Microservice.Logic/Orders/OrderMapper.cs | 22 ++++++---- .../Microservice.MessageBus.csproj | 7 +++ .../Orders/Events/OrderCreatedEvent.cs | 16 +++---- .../Publishers/OrderCreatedEventPublisher.cs | 34 +++++++------- .../Publishers/OrderUpdatedEventPublisher.cs | 34 +++++++------- .../Subscribers/OrderPlacedSubscription.cs | 44 ------------------- .../IMessageBrokerSubscriptionHandler .cs | 5 ++- .../MessageBrokerSubscriptionHandler.cs | 9 ++-- 13 files changed, 100 insertions(+), 102 deletions(-) rename {Microservice.MessageBus/Orders => Microservice.Logic/Orders/Messagebus}/Events/OrderPlacedSubscriptionEvent.cs (63%) create mode 100644 Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs delete mode 100644 Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs diff --git a/Microservice.Api/Microservice.Api.csproj b/Microservice.Api/Microservice.Api.csproj index 8fc63a0..7a1146e 100644 --- a/Microservice.Api/Microservice.Api.csproj +++ b/Microservice.Api/Microservice.Api.csproj @@ -24,6 +24,7 @@ + diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index c06b5d8..fc30a67 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -6,6 +6,7 @@ using Microservice.Logic.Configuration; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; +using Microservice.RabbitMessageBrokerHelpers.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -42,6 +43,11 @@ public void ConfigureServices(IServiceCollection services) .AddMediatR(typeof(LogicServiceCollectionExtensions).Assembly) .AddMessageBroker(_configuration.GetSection("MessageBrokerSettings")) .AddBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) +// .AddMessageBrokerSubscriptions(x => +// x +// .UsePool("Orders") +//// .Subscribe("OrderPlaced") +// ) ; } diff --git a/Microservice.Logic/Microservice.Logic.csproj b/Microservice.Logic/Microservice.Logic.csproj index dd893da..091ae9a 100644 --- a/Microservice.Logic/Microservice.Logic.csproj +++ b/Microservice.Logic/Microservice.Logic.csproj @@ -6,7 +6,7 @@ - + diff --git a/Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs b/Microservice.Logic/Orders/Messagebus/Events/OrderPlacedSubscriptionEvent.cs similarity index 63% rename from Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs rename to Microservice.Logic/Orders/Messagebus/Events/OrderPlacedSubscriptionEvent.cs index 36ccfe3..711d147 100644 --- a/Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs +++ b/Microservice.Logic/Orders/Messagebus/Events/OrderPlacedSubscriptionEvent.cs @@ -1,4 +1,4 @@ -namespace Microservice.MessageBus.Orders.Events +namespace Microservice.Logic.Orders.Messagebus.Events { public class OrderPlacedSubscriptionEvent { diff --git a/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs b/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs new file mode 100644 index 0000000..c43c45a --- /dev/null +++ b/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs @@ -0,0 +1,20 @@ +using System.Threading; +using System.Threading.Tasks; +using MediatR; +using Microservice.Logic.Orders.Messagebus.Events; +using Microservice.RabbitMessageBrokerHelpers.Handlers; + +namespace Microservice.Logic.Orders.Messagebus.SubscriptionHandlers +{ + public class OrderPlacedSubscriptionHandler : MessageBrokerSubscriptionHandler + { + private readonly IMediator _mediator; + public OrderPlacedSubscriptionHandler(IMediator mediator) + { + _mediator = mediator; + } + + public override Task Handle(OrderPlacedSubscriptionEvent eventModel, CancellationToken cancellationToken) + => _mediator.Send(eventModel.To,cancellationToken); + } +} diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 9b401fa..39f10bd 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -1,4 +1,5 @@ using Microservice.Db.EntityModels; +using Microservice.Logic.Orders.Messagebus.Events; using Microservice.Logic.Orders.Models; using Microservice.Logic.Orders.Responses; using Microservice.MessageBus.Orders.Events; @@ -28,16 +29,21 @@ public static OrderPatchModel ToPatchModal(this Order order) return result; } - public static OrderCreatedEvent ToCreatedEvent(this Order order) + public static OrderPlacedCommand ToOrderPlacedCommand( + this OrderPlacedSubscriptionEvent orderPlacedSubscriptionEvent) { - var result = new OrderCreatedEvent - { - Name = order.Name, - Id = order.Id - }; + //TODO CONTINUE HERE + } - return result; + //public static OrderCreatedEvent ToCreatedEvent(this Order order) + //{ + // var result = new OrderCreatedEvent + // { + // Name = order.Name, + // Id = order.Id + // }; - } + // return result; + //} } } diff --git a/Microservice.MessageBus/Microservice.MessageBus.csproj b/Microservice.MessageBus/Microservice.MessageBus.csproj index 6383d06..a9ee4c4 100644 --- a/Microservice.MessageBus/Microservice.MessageBus.csproj +++ b/Microservice.MessageBus/Microservice.MessageBus.csproj @@ -6,10 +6,17 @@ + + + + + + + diff --git a/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs b/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs index 45bfb14..4567283 100644 --- a/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs +++ b/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs @@ -1,8 +1,8 @@ -namespace Microservice.MessageBus.Orders.Events -{ - public class OrderCreatedEvent - { - public long Id { get; set; } - public string Name { get; set; } - } -} +//namespace Microservice.MessageBus.Orders.Events +//{ +// public class OrderCreatedEvent +// { +// public long Id { get; set; } +// public string Name { get; set; } +// } +//} diff --git a/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs b/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs index c0531f6..f45712d 100644 --- a/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs +++ b/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs @@ -1,20 +1,20 @@ -using System.Threading.Tasks; -using Microservice.RabbitMessageBroker; +//using System.Threading.Tasks; +//using Microservice.RabbitMessageBroker; -namespace Microservice.MessageBus.Orders.Publishers -{ - public class OrderCreatedEventPublisher: IMessageBusPublisher - { - private IRabbitMessageBrokerClient BrokerClient { get; } +//namespace Microservice.MessageBus.Orders.Publishers +//{ +// public class OrderCreatedEventPublisher: IMessageBusPublisher +// { +// private IRabbitMessageBrokerClient BrokerClient { get; } - public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) - { - BrokerClient = brokerClient; - } +// public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) +// { +// BrokerClient = brokerClient; +// } - public async Task Publish(TMessage createdEvent) - { - await BrokerClient.Publish("OrderCreated", createdEvent); - } - } -} +// public async Task Publish(TMessage createdEvent) +// { +// await BrokerClient.Publish("OrderCreated", createdEvent); +// } +// } +//} diff --git a/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs b/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs index 16e5529..9be6982 100644 --- a/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs +++ b/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs @@ -1,20 +1,20 @@ -using System.Threading.Tasks; -using Microservice.RabbitMessageBroker; +//using System.Threading.Tasks; +//using Microservice.RabbitMessageBroker; -namespace Microservice.MessageBus.Orders.Publishers -{ - public class OrderUpdatedEventPublisher: IMessageBusPublisher - { - private IRabbitMessageBrokerClient BrokerClient { get; } +//namespace Microservice.MessageBus.Orders.Publishers +//{ +// public class OrderUpdatedEventPublisher: IMessageBusPublisher +// { +// private IRabbitMessageBrokerClient BrokerClient { get; } - public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) - { - BrokerClient = brokerClient; - } +// public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) +// { +// BrokerClient = brokerClient; +// } - public async Task Publish(TMessage message) - { - await BrokerClient.Publish("OrderUpdated", message); - } - } -} +// public async Task Publish(TMessage message) +// { +// await BrokerClient.Publish("OrderUpdated", message); +// } +// } +//} diff --git a/Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs b/Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs deleted file mode 100644 index 6f7c3e2..0000000 --- a/Microservice.MessageBus/Orders/Subscribers/OrderPlacedSubscription.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Microservice.MessageBus.Orders.Events; -using Microservice.RabbitMessageBroker; -using Microsoft.Extensions.Hosting; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Microservice.Api.MessageBus.OrderSubscriptions.Subscriptions -{ - public class OrderPlacedSubscription : IHostedService - { - private readonly IRabbitMessageBrokerClient _messageBrokerClient; - private readonly IServiceProvider _serviceProvider; - public OrderPlacedSubscription(IRabbitMessageBrokerClient messageBrokerClient, IServiceProvider serviceProvider) - { - _messageBrokerClient = messageBrokerClient; - _serviceProvider = serviceProvider; - } - - public Task StartAsync(CancellationToken cancellationToken) - { - return _messageBrokerClient.Subscribe("OrderPlaced", "Orders", HandleMessage, c => c.UseBasicQos()); - } - - private async Task HandleMessage(OrderPlacedSubscriptionEvent orderPlacedSubscriptionEvent) - { - //using (var scope = _serviceProvider.CreateScope()) - //{ - // var serviceProvider = scope.ServiceProvider; - // var mergeSellerLeadsForPeopleManager = serviceProvider.GetService(); - // await _backgroundProcessingClient.Run( - // () => mergeSellerLeadsForPeopleManager.Merge( - // message.NewPersonId, - // message.OldPersonId, - // message.ActionedById)); - //} - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - } -} diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs index 511674f..d27b01e 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs @@ -1,9 +1,10 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace Microservice.RabbitMessageBrokerHelpers.Handlers { public interface IMessageBrokerSubscriptionHandler { - Task HandleEventModel(object eventModel); + Task HandleEventModel(object eventModel, CancellationToken cancellationToken); } } diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs index a101e60..0e4c8b2 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs @@ -1,14 +1,15 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace Microservice.RabbitMessageBrokerHelpers.Handlers { public abstract class MessageBrokerSubscriptionHandler : IMessageBrokerSubscriptionHandler where TEventModel : class { - public Task HandleEventModel(object eventModel) + public Task HandleEventModel(object eventModel, CancellationToken cancellationToken) { - return Handle(eventModel as TEventModel); + return Handle(eventModel as TEventModel, cancellationToken); } - public abstract Task Handle(TEventModel eventModel); + public abstract Task Handle(TEventModel eventModel, CancellationToken cancellationToken); } } From 0f4892b8b766190cd16d02f20eeabe9695e02ca0 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 27 Apr 2020 16:09:25 +0200 Subject: [PATCH 07/37] Delete CorsServiceCollectionExtensions.cs --- .../CorsServiceCollectionExtensions.cs | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 Microservice.Api/Conficuration/CorsServiceCollectionExtensions.cs diff --git a/Microservice.Api/Conficuration/CorsServiceCollectionExtensions.cs b/Microservice.Api/Conficuration/CorsServiceCollectionExtensions.cs deleted file mode 100644 index f6970f6..0000000 --- a/Microservice.Api/Conficuration/CorsServiceCollectionExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace Microservice.Api.Conficuration -{ - public static class CorsServiceCollectionExtensions - { - public static IServiceCollection AddCors(this IServiceCollection services, - IConfiguration config) - { - services.AddCors(options => options.AddPolicy("Default", builder => - { - builder - .AllowAnyOrigin() - .AllowAnyMethod() - .AllowAnyHeader(); - })); - - return services; - } - } -} From 69e43e09e7e6320f95725ca89061e6eb6cee43f5 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 27 Apr 2020 16:09:31 +0200 Subject: [PATCH 08/37] Create CorsServiceCollectionExtensions.cs --- .../CorsServiceCollectionExtensions.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Microservice.Api/Configuration/CorsServiceCollectionExtensions.cs diff --git a/Microservice.Api/Configuration/CorsServiceCollectionExtensions.cs b/Microservice.Api/Configuration/CorsServiceCollectionExtensions.cs new file mode 100644 index 0000000..75d316f --- /dev/null +++ b/Microservice.Api/Configuration/CorsServiceCollectionExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace Microservice.Api.Configuration +{ + public static class CorsServiceCollectionExtensions + { + public static IServiceCollection AddCorsRules(this IServiceCollection services) + { + services.AddCors(options => options.AddPolicy("Default", builder => + { + builder + .AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + })); + + return services; + } + } +} From b565b555b2463631dbf4b22e43b7ea54695d9ed6 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 27 Apr 2020 16:09:33 +0200 Subject: [PATCH 09/37] Update Startup.cs --- Microservice.Api/Startup.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index fc30a67..3afd219 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -1,12 +1,12 @@ using FluentValidation.AspNetCore; using MediatR; +using Microservice.Api.Configuration; using Microservice.Api.Filters; using Microservice.Db.Configuration; using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; -using Microservice.RabbitMessageBrokerHelpers.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -27,7 +27,7 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { services - .AddCors() + .AddCorsRules() .AddControllers() .AddNewtonsoftJson() ; From 28d51ba3f5145bd6071982344f903c8ec282a817 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 27 Apr 2020 22:57:00 +0200 Subject: [PATCH 10/37] Added Calcs --- Microservice.Api/Startup.cs | 13 ++++++---- .../Orders/Commands/PlacedOrderCommand.cs | 11 +++++++++ .../Orders/Handlers/PlacedOrderHandler.cs | 21 ++++++++++++++++ .../OrderPlacedSubscriptionHandler.cs | 8 +++---- Microservice.Logic/Orders/OrderMapper.cs | 24 +++++++------------ 5 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs create mode 100644 Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 3afd219..624a25d 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -5,8 +5,11 @@ using Microservice.Db.Configuration; using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; +using Microservice.Logic.Orders.Handlers; +using Microservice.Logic.Orders.Messagebus.Events; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; +using Microservice.RabbitMessageBrokerHelpers.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -43,11 +46,11 @@ public void ConfigureServices(IServiceCollection services) .AddMediatR(typeof(LogicServiceCollectionExtensions).Assembly) .AddMessageBroker(_configuration.GetSection("MessageBrokerSettings")) .AddBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) -// .AddMessageBrokerSubscriptions(x => -// x -// .UsePool("Orders") -//// .Subscribe("OrderPlaced") -// ) + .AddMessageBrokerSubscriptions(x => + x + .UsePool("Orders") + .Subscribe("OrderPlaced") + ) ; } diff --git a/Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs b/Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs new file mode 100644 index 0000000..cca6e21 --- /dev/null +++ b/Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs @@ -0,0 +1,11 @@ +using MediatR; +using Microservice.Logic.Orders.Responses; + +namespace Microservice.Logic.Orders.Commands +{ + public class PlacedOrderCommand : IRequest + { + public long Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs new file mode 100644 index 0000000..5f4f920 --- /dev/null +++ b/Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs @@ -0,0 +1,21 @@ +using MediatR; +using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.Responses; +using System.Threading; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.Handlers +{ + public class PlacedOrderHandler : IRequestHandler + { + + public PlacedOrderHandler() + { + + } + public Task Handle(PlacedOrderCommand request, CancellationToken cancellationToken) + { + throw new System.NotImplementedException(); + } + } +} diff --git a/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs b/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs index c43c45a..a4c50ca 100644 --- a/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs +++ b/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs @@ -1,8 +1,8 @@ -using System.Threading; -using System.Threading.Tasks; -using MediatR; +using MediatR; using Microservice.Logic.Orders.Messagebus.Events; using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading; +using System.Threading.Tasks; namespace Microservice.Logic.Orders.Messagebus.SubscriptionHandlers { @@ -15,6 +15,6 @@ public OrderPlacedSubscriptionHandler(IMediator mediator) } public override Task Handle(OrderPlacedSubscriptionEvent eventModel, CancellationToken cancellationToken) - => _mediator.Send(eventModel.To,cancellationToken); + => _mediator.Send(eventModel.ToOrderPlacedCommand(), cancellationToken); } } diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 39f10bd..9583336 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -1,8 +1,8 @@ using Microservice.Db.EntityModels; +using Microservice.Logic.Orders.Commands; using Microservice.Logic.Orders.Messagebus.Events; using Microservice.Logic.Orders.Models; using Microservice.Logic.Orders.Responses; -using Microservice.MessageBus.Orders.Events; namespace Microservice.Logic.Orders { @@ -29,21 +29,15 @@ public static OrderPatchModel ToPatchModal(this Order order) return result; } - public static OrderPlacedCommand ToOrderPlacedCommand( - this OrderPlacedSubscriptionEvent orderPlacedSubscriptionEvent) + public static PlacedOrderCommand ToOrderPlacedCommand( + this OrderPlacedSubscriptionEvent subscriptionEvent) { - //TODO CONTINUE HERE - } - - //public static OrderCreatedEvent ToCreatedEvent(this Order order) - //{ - // var result = new OrderCreatedEvent - // { - // Name = order.Name, - // Id = order.Id - // }; + var result = new PlacedOrderCommand + { + Id = subscriptionEvent.Id + }; - // return result; - //} + return result; + } } } From 35081ccb230a5d2e788c9dc754353090249cc10c Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Wed, 29 Apr 2020 08:33:17 +0200 Subject: [PATCH 11/37] Think its working --- Microservice.Api/Startup.cs | 6 ++--- Microservice.Logic/Microservice.Logic.csproj | 2 +- .../OrderCreatedEventPublisher.cs | 22 +++++++++++++++ .../OrderUpdatedEventPublisher.cs | 19 +++++++++++++ .../Orders/Events/OrderCreatedEvent.cs | 8 ++++++ .../Events/OrderPlacedSubscriptionEvent.cs | 7 +++++ .../Orders/Events/OrderUpdatedEvent.cs | 8 ++++++ .../Orders/Handlers/CreateOrderHandler.cs | 7 +++-- .../Orders/Handlers/PatchOrderHandler.cs | 11 +++++--- .../Orders/Handlers/PlacedOrderHandler.cs | 21 --------------- .../OrderPlacedSubscriptionHandler.cs | 20 -------------- Microservice.Logic/Orders/OrderMapper.cs | 26 +++++++++++++++++- .../PlacedOrderSubscriptionHandler.cs | 27 +++++++++++++++++++ .../Microservice.MessageBus.csproj | 4 --- .../OrderCreatedEventPublisher.cs | 18 +++++++++++++ .../OrderUpdatedEventPublisher.cs | 18 +++++++++++++ .../Orders/Events/OrderCreatedEvent.cs | 16 +++++------ .../Events/OrderPlacedSubscriptionEvent.cs | 0 .../Orders/Events/OrderUpdatedEvent.cs | 8 ++++++ .../Publishers/OrderCreatedEventPublisher.cs | 20 -------------- .../Publishers/OrderUpdatedEventPublisher.cs | 20 -------------- .../PlacedOrderSubscriptionHandler.cs | 27 +++++++++++++++++++ .../RabbitMessageBrokerClient.cs | 8 +++--- .../Handlers/EventPublisher.cs | 9 +++++++ .../IMessageBrokerSubscriptionHandler .cs | 5 ++-- .../MessageBrokerSubscriptionHandler.cs | 9 +++---- .../Models/MessageBrokerSubscription.cs | 4 +-- Microservice.sln | 8 +----- 28 files changed, 231 insertions(+), 127 deletions(-) create mode 100644 Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs create mode 100644 Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs create mode 100644 Microservice.Logic/Orders/Events/OrderCreatedEvent.cs create mode 100644 Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs create mode 100644 Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs delete mode 100644 Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs delete mode 100644 Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs create mode 100644 Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs create mode 100644 Microservice.MessageBus/Orders/EventPublishers/OrderCreatedEventPublisher.cs create mode 100644 Microservice.MessageBus/Orders/EventPublishers/OrderUpdatedEventPublisher.cs rename {Microservice.Logic/Orders/Messagebus => Microservice.MessageBus/Orders}/Events/OrderPlacedSubscriptionEvent.cs (100%) create mode 100644 Microservice.MessageBus/Orders/Events/OrderUpdatedEvent.cs delete mode 100644 Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs delete mode 100644 Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs create mode 100644 Microservice.MessageBus/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Handlers/EventPublisher.cs diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 624a25d..6705ca4 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -5,8 +5,8 @@ using Microservice.Db.Configuration; using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; -using Microservice.Logic.Orders.Handlers; -using Microservice.Logic.Orders.Messagebus.Events; +using Microservice.Logic.Orders.Events; +using Microservice.Logic.Orders.SubscriptionHandlers; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; using Microservice.RabbitMessageBrokerHelpers.Configuration; @@ -49,7 +49,7 @@ public void ConfigureServices(IServiceCollection services) .AddMessageBrokerSubscriptions(x => x .UsePool("Orders") - .Subscribe("OrderPlaced") + .Subscribe("OrderPlaced") ) ; } diff --git a/Microservice.Logic/Microservice.Logic.csproj b/Microservice.Logic/Microservice.Logic.csproj index 091ae9a..9e3e999 100644 --- a/Microservice.Logic/Microservice.Logic.csproj +++ b/Microservice.Logic/Microservice.Logic.csproj @@ -12,6 +12,6 @@ - + diff --git a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs new file mode 100644 index 0000000..1bacb81 --- /dev/null +++ b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs @@ -0,0 +1,22 @@ +using Microservice.Logic.Orders.Events; +using Microservice.RabbitMessageBroker; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.EventPublishers +{ + public class OrderCreatedEventPublisher : EventPublisher + { + private IRabbitMessageBrokerClient BrokerClient { get; } + + public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) + { + BrokerClient = brokerClient; + } + + public override async Task Publish(OrderCreatedEvent eventModel) + => await BrokerClient.Publish("OrderCreated", eventModel); + } + + +} diff --git a/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs new file mode 100644 index 0000000..6473cba --- /dev/null +++ b/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs @@ -0,0 +1,19 @@ +using Microservice.Logic.Orders.Events; +using Microservice.RabbitMessageBroker; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.EventPublishers +{ + public class OrderUpdatedEventPublisher : EventPublisher + { + private IRabbitMessageBrokerClient BrokerClient { get; } + + public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) + { + BrokerClient = brokerClient; + } + public override async Task Publish(OrderUpdatedEvent eventModel) + => await BrokerClient.Publish("OrderUpdated", eventModel); + } +} diff --git a/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs b/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs new file mode 100644 index 0000000..210bd6f --- /dev/null +++ b/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs @@ -0,0 +1,8 @@ +namespace Microservice.Logic.Orders.Events +{ + public class OrderCreatedEvent + { + public long Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs b/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs new file mode 100644 index 0000000..2fed0f6 --- /dev/null +++ b/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs @@ -0,0 +1,7 @@ +namespace Microservice.Logic.Orders.Events +{ + public class OrderPlacedSubscriptionEvent + { + public long Id { get; set; } + } +} diff --git a/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs b/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs new file mode 100644 index 0000000..bba7cb5 --- /dev/null +++ b/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs @@ -0,0 +1,8 @@ +namespace Microservice.Logic.Orders.Events +{ + public class OrderUpdatedEvent + { + public long Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs index 6d9b160..169ae60 100644 --- a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs @@ -2,9 +2,8 @@ using Microservice.Db; using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.EventPublishers; using Microservice.Logic.Orders.Responses; -using Microservice.MessageBus; -using Microservice.MessageBus.Orders.Publishers; using System.Threading; using System.Threading.Tasks; @@ -13,9 +12,9 @@ namespace Microservice.Logic.Orders.Handlers public class CreateOrderHandler : IRequestHandler { private readonly MicroserviceDbContext _dbContext; - private readonly IMessageBusPublisher _createdEventPublisher; + private readonly OrderCreatedEventPublisher _createdEventPublisher; - public CreateOrderHandler(MicroserviceDbContext dbContext, IMessageBusPublisher createdEventPublisher) + public CreateOrderHandler(MicroserviceDbContext dbContext, OrderCreatedEventPublisher createdEventPublisher) { _dbContext = dbContext; _createdEventPublisher = createdEventPublisher; diff --git a/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs index d8d9dd2..8ed6ca4 100644 --- a/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs @@ -2,6 +2,7 @@ using Microservice.Db; using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.EventPublishers; using Microservice.Logic.Orders.Responses; using Microsoft.EntityFrameworkCore; using System.Diagnostics; @@ -10,13 +11,15 @@ namespace Microservice.Logic.Orders.Handlers { - public class PatchOrderHandler : IRequestHandler + public class PatchOrderHandler : IRequestHandler { private readonly MicroserviceDbContext _dbContext; + private readonly OrderUpdatedEventPublisher _updatedEventPublisher; - public PatchOrderHandler(MicroserviceDbContext dbContext) + public PatchOrderHandler(MicroserviceDbContext dbContext, OrderUpdatedEventPublisher updatedEventPublisher) { _dbContext = dbContext; + _updatedEventPublisher = updatedEventPublisher; } public async Task Handle(PatchOrderCommand command, CancellationToken cancellationToken) @@ -47,11 +50,13 @@ await _dbContext.Orders { Id = originalEntity.Id, Name = model.Name - }; + }; _dbContext.Update(updatedEntity); await _dbContext.SaveChangesAsync(cancellationToken); + await _updatedEventPublisher.Publish(updatedEntity.ToUpdatedEvent()); + var response = updatedEntity.ToResponse(); return response; } diff --git a/Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs deleted file mode 100644 index 5f4f920..0000000 --- a/Microservice.Logic/Orders/Handlers/PlacedOrderHandler.cs +++ /dev/null @@ -1,21 +0,0 @@ -using MediatR; -using Microservice.Logic.Orders.Commands; -using Microservice.Logic.Orders.Responses; -using System.Threading; -using System.Threading.Tasks; - -namespace Microservice.Logic.Orders.Handlers -{ - public class PlacedOrderHandler : IRequestHandler - { - - public PlacedOrderHandler() - { - - } - public Task Handle(PlacedOrderCommand request, CancellationToken cancellationToken) - { - throw new System.NotImplementedException(); - } - } -} diff --git a/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs b/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs deleted file mode 100644 index a4c50ca..0000000 --- a/Microservice.Logic/Orders/Messagebus/SubscriptionHandlers/OrderPlacedSubscriptionHandler.cs +++ /dev/null @@ -1,20 +0,0 @@ -using MediatR; -using Microservice.Logic.Orders.Messagebus.Events; -using Microservice.RabbitMessageBrokerHelpers.Handlers; -using System.Threading; -using System.Threading.Tasks; - -namespace Microservice.Logic.Orders.Messagebus.SubscriptionHandlers -{ - public class OrderPlacedSubscriptionHandler : MessageBrokerSubscriptionHandler - { - private readonly IMediator _mediator; - public OrderPlacedSubscriptionHandler(IMediator mediator) - { - _mediator = mediator; - } - - public override Task Handle(OrderPlacedSubscriptionEvent eventModel, CancellationToken cancellationToken) - => _mediator.Send(eventModel.ToOrderPlacedCommand(), cancellationToken); - } -} diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 9583336..413fb54 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -1,6 +1,6 @@ using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Commands; -using Microservice.Logic.Orders.Messagebus.Events; +using Microservice.Logic.Orders.Events; using Microservice.Logic.Orders.Models; using Microservice.Logic.Orders.Responses; @@ -39,5 +39,29 @@ public static PlacedOrderCommand ToOrderPlacedCommand( return result; } + + public static OrderUpdatedEvent ToUpdatedEvent( + this Order order) + { + var result = new OrderUpdatedEvent + { + Id = order.Id, + Name = order.Name + }; + + return result; + } + + public static OrderCreatedEvent ToCreatedEvent( + this Order order) + { + var result = new OrderCreatedEvent + { + Id = order.Id, + Name = order.Name + }; + + return result; + } } } diff --git a/Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs b/Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs new file mode 100644 index 0000000..5af95d0 --- /dev/null +++ b/Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs @@ -0,0 +1,27 @@ +using MediatR; +using Microservice.Logic.Orders.Events; +using Microservice.Logic.Orders.Queries; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.SubscriptionHandlers +{ + public class PlacedOrderSubscriptionHandler : MessageBrokerSubscriptionHandler + { + private readonly IMediator _mediator; + public PlacedOrderSubscriptionHandler(IMediator mediator) + { + _mediator = mediator; + } + + public override async Task Handle(OrderPlacedSubscriptionEvent eventModel) + { + var order = await _mediator.Send(new GetOrderByIdQuery(eventModel.Id)); + + if (order == null) + return; + + // Do some custom operation here + } + } +} diff --git a/Microservice.MessageBus/Microservice.MessageBus.csproj b/Microservice.MessageBus/Microservice.MessageBus.csproj index a9ee4c4..40a6b91 100644 --- a/Microservice.MessageBus/Microservice.MessageBus.csproj +++ b/Microservice.MessageBus/Microservice.MessageBus.csproj @@ -15,8 +15,4 @@ - - - - diff --git a/Microservice.MessageBus/Orders/EventPublishers/OrderCreatedEventPublisher.cs b/Microservice.MessageBus/Orders/EventPublishers/OrderCreatedEventPublisher.cs new file mode 100644 index 0000000..8616fd2 --- /dev/null +++ b/Microservice.MessageBus/Orders/EventPublishers/OrderCreatedEventPublisher.cs @@ -0,0 +1,18 @@ +using Microservice.RabbitMessageBroker; +using System.Threading.Tasks; + +namespace Microservice.MessageBus.Orders.EventPublishers +{ + public class OrderCreatedEventPublisher : IMessageBusPublisher + { + private IRabbitMessageBrokerClient BrokerClient { get; } + + public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) + { + BrokerClient = brokerClient; + } + + public async Task Publish(TMessage createdEvent) + => await BrokerClient.Publish("OrderCreated", createdEvent); + } +} diff --git a/Microservice.MessageBus/Orders/EventPublishers/OrderUpdatedEventPublisher.cs b/Microservice.MessageBus/Orders/EventPublishers/OrderUpdatedEventPublisher.cs new file mode 100644 index 0000000..554b979 --- /dev/null +++ b/Microservice.MessageBus/Orders/EventPublishers/OrderUpdatedEventPublisher.cs @@ -0,0 +1,18 @@ +using Microservice.RabbitMessageBroker; +using System.Threading.Tasks; + +namespace Microservice.MessageBus.Orders.EventPublishers +{ + public class OrderUpdatedEventPublisher : IMessageBusPublisher + { + private IRabbitMessageBrokerClient BrokerClient { get; } + + public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) + { + BrokerClient = brokerClient; + } + + public async Task Publish(TMessage message) + => await BrokerClient.Publish("OrderUpdated", message); + } +} diff --git a/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs b/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs index 4567283..45bfb14 100644 --- a/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs +++ b/Microservice.MessageBus/Orders/Events/OrderCreatedEvent.cs @@ -1,8 +1,8 @@ -//namespace Microservice.MessageBus.Orders.Events -//{ -// public class OrderCreatedEvent -// { -// public long Id { get; set; } -// public string Name { get; set; } -// } -//} +namespace Microservice.MessageBus.Orders.Events +{ + public class OrderCreatedEvent + { + public long Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Microservice.Logic/Orders/Messagebus/Events/OrderPlacedSubscriptionEvent.cs b/Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs similarity index 100% rename from Microservice.Logic/Orders/Messagebus/Events/OrderPlacedSubscriptionEvent.cs rename to Microservice.MessageBus/Orders/Events/OrderPlacedSubscriptionEvent.cs diff --git a/Microservice.MessageBus/Orders/Events/OrderUpdatedEvent.cs b/Microservice.MessageBus/Orders/Events/OrderUpdatedEvent.cs new file mode 100644 index 0000000..b7f8cf3 --- /dev/null +++ b/Microservice.MessageBus/Orders/Events/OrderUpdatedEvent.cs @@ -0,0 +1,8 @@ +namespace Microservice.MessageBus.Orders.Events +{ + public class OrderUpdatedEvent + { + public long Id { get; set; } + public string Name { get; set; } + } +} diff --git a/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs b/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs deleted file mode 100644 index f45712d..0000000 --- a/Microservice.MessageBus/Orders/Publishers/OrderCreatedEventPublisher.cs +++ /dev/null @@ -1,20 +0,0 @@ -//using System.Threading.Tasks; -//using Microservice.RabbitMessageBroker; - -//namespace Microservice.MessageBus.Orders.Publishers -//{ -// public class OrderCreatedEventPublisher: IMessageBusPublisher -// { -// private IRabbitMessageBrokerClient BrokerClient { get; } - -// public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) -// { -// BrokerClient = brokerClient; -// } - -// public async Task Publish(TMessage createdEvent) -// { -// await BrokerClient.Publish("OrderCreated", createdEvent); -// } -// } -//} diff --git a/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs b/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs deleted file mode 100644 index 9be6982..0000000 --- a/Microservice.MessageBus/Orders/Publishers/OrderUpdatedEventPublisher.cs +++ /dev/null @@ -1,20 +0,0 @@ -//using System.Threading.Tasks; -//using Microservice.RabbitMessageBroker; - -//namespace Microservice.MessageBus.Orders.Publishers -//{ -// public class OrderUpdatedEventPublisher: IMessageBusPublisher -// { -// private IRabbitMessageBrokerClient BrokerClient { get; } - -// public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) -// { -// BrokerClient = brokerClient; -// } - -// public async Task Publish(TMessage message) -// { -// await BrokerClient.Publish("OrderUpdated", message); -// } -// } -//} diff --git a/Microservice.MessageBus/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs b/Microservice.MessageBus/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs new file mode 100644 index 0000000..c164f6c --- /dev/null +++ b/Microservice.MessageBus/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs @@ -0,0 +1,27 @@ +using MediatR; +using Microservice.MessageBus.Orders.Events; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading; +using System.Threading.Tasks; + +namespace Microservice.MessageBus.Orders.SubscriptionHandlers +{ + public class PlacedOrderSubscriptionHandler : MessageBrokerSubscriptionHandler + { + private readonly IMediator _mediator; + public PlacedOrderSubscriptionHandler(IMediator mediator) + { + _mediator = mediator; + } + + public override async Task Handle(OrderPlacedSubscriptionEvent eventModel, CancellationToken cancellationToken) + { + var order = await _mediator.Send(new GetOrderByIdQuery(eventModel.Id), cancellationToken); + + if (order == null) + return; + + // Do some custom operation here + } + } +} diff --git a/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs b/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs index afd7b8b..e6bd3b5 100644 --- a/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs +++ b/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs @@ -18,7 +18,7 @@ namespace Microservice.RabbitMessageBroker { public class RabbitMessageBrokerClient : IRabbitMessageBrokerClient { - private IModel _channel { get; set; } + private IModel Channel { get; set; } private readonly ILogger _logger; private readonly IOptions _options; @@ -76,10 +76,10 @@ protected async Task GetChannel(string topic, string subscriptionId = nu if (_options.Value == null) throw new Exception("RabbitMQ: Message broker client credentials not configured {@details}"); - if (_channel == null || _channel.IsClosed) - _channel = await TryConnect(topic, subscriptionId); + if (Channel == null || Channel.IsClosed) + Channel = await TryConnect(topic, subscriptionId); - return _channel; + return Channel; } public Task Subscribe( diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/EventPublisher.cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/EventPublisher.cs new file mode 100644 index 0000000..da921ca --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/EventPublisher.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Microservice.RabbitMessageBrokerHelpers.Handlers +{ + public abstract class EventPublisher where TPublishEvent : class + { + public abstract Task Publish(TPublishEvent eventModel); + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs index d27b01e..511674f 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/IMessageBrokerSubscriptionHandler .cs @@ -1,10 +1,9 @@ -using System.Threading; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace Microservice.RabbitMessageBrokerHelpers.Handlers { public interface IMessageBrokerSubscriptionHandler { - Task HandleEventModel(object eventModel, CancellationToken cancellationToken); + Task HandleEventModel(object eventModel); } } diff --git a/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs b/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs index 0e4c8b2..a101e60 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Handlers/MessageBrokerSubscriptionHandler.cs @@ -1,15 +1,14 @@ -using System.Threading; -using System.Threading.Tasks; +using System.Threading.Tasks; namespace Microservice.RabbitMessageBrokerHelpers.Handlers { public abstract class MessageBrokerSubscriptionHandler : IMessageBrokerSubscriptionHandler where TEventModel : class { - public Task HandleEventModel(object eventModel, CancellationToken cancellationToken) + public Task HandleEventModel(object eventModel) { - return Handle(eventModel as TEventModel, cancellationToken); + return Handle(eventModel as TEventModel); } - public abstract Task Handle(TEventModel eventModel, CancellationToken cancellationToken); + public abstract Task Handle(TEventModel eventModel); } } diff --git a/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs b/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs index c0e13a5..b2c9f17 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Models/MessageBrokerSubscription.cs @@ -1,10 +1,8 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Microservice.RabbitMessageBrokerHelpers.Models { - public class MessageBrokerSubscription + public class MessageBrokerSubscription { public string Pool { get; set; } public Type Handler { get; set; } diff --git a/Microservice.sln b/Microservice.sln index 0b6cb18..923b1df 100644 --- a/Microservice.sln +++ b/Microservice.sln @@ -17,9 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.RabbitMessageB EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.HangfireBackgroundJobServer", "Microservice.HangfireBackgroundJobServer\Microservice.HangfireBackgroundJobServer.csproj", "{96C85DBD-DE58-4D02-A01B-E10D521CB089}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microservice.MessageBus", "Microservice.MessageBus\Microservice.MessageBus.csproj", "{5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microservice.RabbitMessageBrokerHelpers", "Microservice.RabbitMessageBrokerHelpers\Microservice.RabbitMessageBrokerHelpers.csproj", "{CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.RabbitMessageBrokerHelpers", "Microservice.RabbitMessageBrokerHelpers\Microservice.RabbitMessageBrokerHelpers.csproj", "{CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -55,10 +53,6 @@ Global {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Debug|Any CPU.Build.0 = Debug|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Release|Any CPU.ActiveCfg = Release|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Release|Any CPU.Build.0 = Release|Any CPU - {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5E25136B-6AE3-4BA6-86C2-4ABE27D9D0AE}.Release|Any CPU.Build.0 = Release|Any CPU {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Debug|Any CPU.Build.0 = Debug|Any CPU {CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}.Release|Any CPU.ActiveCfg = Release|Any CPU From 0ef4fdda62c54df3fd12f62aacf72ab6f5a42d00 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 05:43:50 +0200 Subject: [PATCH 12/37] Update MessageBrokerSubscriptionBackgroundJob.cs --- .../MessageBrokerSubscriptionBackgroundJob.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs index ff19011..75694e9 100644 --- a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs +++ b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs @@ -23,7 +23,13 @@ public class MessageBrokerSubscriptionBackgroundJob : IHostedService private readonly MessageBrokerSubscriptionsConfigurationBuilder _configurationBuilder; private List _unsubscribeCallbacks; - public MessageBrokerSubscriptionBackgroundJob(MessageBrokerSubscriptionsConfigurationBuilder configurationBuilder, IServiceProvider serviceProvider, IRabbitMessageBrokerClient messageBrokerClient, ILogger logger, List unsubscribeCallbacks, IBackgroundProcessingClient backgroundProcessingClient) + public MessageBrokerSubscriptionBackgroundJob( + MessageBrokerSubscriptionsConfigurationBuilder configurationBuilder, + IServiceProvider serviceProvider, + IRabbitMessageBrokerClient messageBrokerClient, + ILogger logger, + List unsubscribeCallbacks, + IBackgroundProcessingClient backgroundProcessingClient) { _configurationBuilder = configurationBuilder; _serviceProvider = serviceProvider; From 90a4a9ab5312c95a6175800cbd34f245068f701f Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 05:50:16 +0200 Subject: [PATCH 13/37] Update OrdersController.cs --- Microservice.Api/Controllers/OrdersController.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Microservice.Api/Controllers/OrdersController.cs b/Microservice.Api/Controllers/OrdersController.cs index 7b44ffc..f6374b1 100644 --- a/Microservice.Api/Controllers/OrdersController.cs +++ b/Microservice.Api/Controllers/OrdersController.cs @@ -1,11 +1,10 @@ using MediatR; -using Microsoft.AspNetCore.JsonPatch; -using Microsoft.AspNetCore.Mvc; -using System.Threading.Tasks; using Microservice.Logic.Orders.Commands; using Microservice.Logic.Orders.Models; using Microservice.Logic.Orders.Queries; -using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.AspNetCore.JsonPatch; +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; namespace Microservice.Api.Controllers { From 22ae929b86535ee8fd13a2b5d50238249f90d2ed Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:07:17 +0200 Subject: [PATCH 14/37] Wire up subscriptions --- ...derCommand.cs => PutPlacedOrderCommand.cs} | 4 +-- .../OrderCreatedEventPublisher.cs | 2 -- .../OrderPlacedEventSubscription.cs | 22 +++++++++++++++ .../Events/OrderPlacedSubscriptionEvent.cs | 1 + .../Orders/Handlers/PutPlaceOrderHandler.cs | 23 ++++++++++++++++ Microservice.Logic/Orders/OrderMapper.cs | 7 ++--- .../PlacedOrderSubscriptionHandler.cs | 27 ------------------- 7 files changed, 52 insertions(+), 34 deletions(-) rename Microservice.Logic/Orders/Commands/{PlacedOrderCommand.cs => PutPlacedOrderCommand.cs} (59%) create mode 100644 Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs create mode 100644 Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs delete mode 100644 Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs diff --git a/Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs b/Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs similarity index 59% rename from Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs rename to Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs index cca6e21..583a452 100644 --- a/Microservice.Logic/Orders/Commands/PlacedOrderCommand.cs +++ b/Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs @@ -3,9 +3,9 @@ namespace Microservice.Logic.Orders.Commands { - public class PlacedOrderCommand : IRequest + public class PutPlacedOrderCommand : IRequest { public long Id { get; set; } - public string Name { get; set; } + public int Quantity { get; set; } } } diff --git a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs index 1bacb81..7cee824 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs @@ -17,6 +17,4 @@ public OrderCreatedEventPublisher(IRabbitMessageBrokerClient brokerClient) public override async Task Publish(OrderCreatedEvent eventModel) => await BrokerClient.Publish("OrderCreated", eventModel); } - - } diff --git a/Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs b/Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs new file mode 100644 index 0000000..0e74e51 --- /dev/null +++ b/Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs @@ -0,0 +1,22 @@ +using MediatR; +using Microservice.Logic.Orders.Events; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.EventSubscriptions +{ + public class OrderPlacedEventSubscription : MessageBrokerSubscriptionHandler + { + private readonly IMediator _mediator; + + public OrderPlacedEventSubscription(IMediator mediator) + { + _mediator = mediator; + } + + public override async Task Handle(OrderPlacedSubscriptionEvent eventModel) + { + await _mediator.Send(eventModel.ToPutPlacedOrderCommand()); + } + } +} diff --git a/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs b/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs index 2fed0f6..edcc391 100644 --- a/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs +++ b/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs @@ -3,5 +3,6 @@ public class OrderPlacedSubscriptionEvent { public long Id { get; set; } + public int Quantity { get; set; } } } diff --git a/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs new file mode 100644 index 0000000..a5ead58 --- /dev/null +++ b/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs @@ -0,0 +1,23 @@ +using System.Threading; +using System.Threading.Tasks; +using MediatR; +using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.Responses; + +namespace Microservice.Logic.Orders.Handlers +{ + public class PutPlaceOrderHandler: IRequestHandler + { + private readonly IMediator _mediator; + + public PutPlaceOrderHandler(IMediator mediator) + { + _mediator = mediator; + } + + public Task Handle(PutPlacedOrderCommand request, CancellationToken cancellationToken) + { + throw new System.NotImplementedException(); + } + } +} diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 413fb54..53977be 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -29,12 +29,13 @@ public static OrderPatchModel ToPatchModal(this Order order) return result; } - public static PlacedOrderCommand ToOrderPlacedCommand( + public static PutPlacedOrderCommand ToPutPlacedOrderCommand( this OrderPlacedSubscriptionEvent subscriptionEvent) { - var result = new PlacedOrderCommand + var result = new PutPlacedOrderCommand { - Id = subscriptionEvent.Id + Id = subscriptionEvent.Id, + Quantity = subscriptionEvent.Quantity }; return result; diff --git a/Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs b/Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs deleted file mode 100644 index 5af95d0..0000000 --- a/Microservice.Logic/Orders/SubscriptionHandlers/PlacedOrderSubscriptionHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using MediatR; -using Microservice.Logic.Orders.Events; -using Microservice.Logic.Orders.Queries; -using Microservice.RabbitMessageBrokerHelpers.Handlers; -using System.Threading.Tasks; - -namespace Microservice.Logic.Orders.SubscriptionHandlers -{ - public class PlacedOrderSubscriptionHandler : MessageBrokerSubscriptionHandler - { - private readonly IMediator _mediator; - public PlacedOrderSubscriptionHandler(IMediator mediator) - { - _mediator = mediator; - } - - public override async Task Handle(OrderPlacedSubscriptionEvent eventModel) - { - var order = await _mediator.Send(new GetOrderByIdQuery(eventModel.Id)); - - if (order == null) - return; - - // Do some custom operation here - } - } -} From 937fd35496773c9528d77956e0b0bd382906187f Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:36:01 +0200 Subject: [PATCH 15/37] Add Order quantity --- Microservice.Db/EntityModels/Order.cs | 1 + ...43519_AddOrderQuantityProperty.Designer.cs | 43 +++++++++++++++++++ ...20200504043519_AddOrderQuantityProperty.cs | 23 ++++++++++ .../MicroserviceDbContextModelSnapshot.cs | 5 ++- .../Orders/Events/OrderCreatedEvent.cs | 1 + .../Orders/Events/OrderUpdatedEvent.cs | 1 + Microservice.Logic/Orders/OrderMapper.cs | 9 ++-- .../Orders/Responses/OrderResponse.cs | 1 + 8 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.Designer.cs create mode 100644 Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.cs diff --git a/Microservice.Db/EntityModels/Order.cs b/Microservice.Db/EntityModels/Order.cs index 98d044c..61f2d25 100644 --- a/Microservice.Db/EntityModels/Order.cs +++ b/Microservice.Db/EntityModels/Order.cs @@ -4,5 +4,6 @@ public class Order { public long Id { get; set; } public string Name { get; set; } + public int Quantity { get; set; } } } diff --git a/Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.Designer.cs b/Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.Designer.cs new file mode 100644 index 0000000..a827c8c --- /dev/null +++ b/Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.Designer.cs @@ -0,0 +1,43 @@ +// +using Microservice.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace Microservice.Db.Migrations +{ + [DbContext(typeof(MicroserviceDbContext))] + [Migration("20200504043519_AddOrderQuantityProperty")] + partial class AddOrderQuantityProperty + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) + .HasAnnotation("ProductVersion", "3.1.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("Microservice.Db.EntityModels.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Quantity") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.cs b/Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.cs new file mode 100644 index 0000000..5b699ba --- /dev/null +++ b/Microservice.Db/Migrations/20200504043519_AddOrderQuantityProperty.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Microservice.Db.Migrations +{ + public partial class AddOrderQuantityProperty : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Quantity", + table: "Orders", + nullable: false, + defaultValue: 0); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Quantity", + table: "Orders"); + } + } +} diff --git a/Microservice.Db/Migrations/MicroserviceDbContextModelSnapshot.cs b/Microservice.Db/Migrations/MicroserviceDbContextModelSnapshot.cs index 97600d9..eb35d4d 100644 --- a/Microservice.Db/Migrations/MicroserviceDbContextModelSnapshot.cs +++ b/Microservice.Db/Migrations/MicroserviceDbContextModelSnapshot.cs @@ -15,7 +15,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) - .HasAnnotation("ProductVersion", "3.1.0") + .HasAnnotation("ProductVersion", "3.1.3") .HasAnnotation("Relational:MaxIdentifierLength", 63); modelBuilder.Entity("Microservice.Db.EntityModels.Order", b => @@ -28,6 +28,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("Name") .HasColumnType("text"); + b.Property("Quantity") + .HasColumnType("integer"); + b.HasKey("Id"); b.ToTable("Orders"); diff --git a/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs b/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs index 210bd6f..a8be5d4 100644 --- a/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs +++ b/Microservice.Logic/Orders/Events/OrderCreatedEvent.cs @@ -4,5 +4,6 @@ public class OrderCreatedEvent { public long Id { get; set; } public string Name { get; set; } + public int Quantity { get; set; } } } diff --git a/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs b/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs index bba7cb5..03b2da1 100644 --- a/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs +++ b/Microservice.Logic/Orders/Events/OrderUpdatedEvent.cs @@ -4,5 +4,6 @@ public class OrderUpdatedEvent { public long Id { get; set; } public string Name { get; set; } + public int Quantity { get; set; } } } diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 53977be..9752aa9 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -13,7 +13,8 @@ public static OrderResponse ToResponse(this Order order) var result = new OrderResponse { Id = order.Id, - Name = order.Name + Name = order.Name, + Quantity = order.Quantity }; return result; @@ -47,7 +48,8 @@ public static OrderUpdatedEvent ToUpdatedEvent( var result = new OrderUpdatedEvent { Id = order.Id, - Name = order.Name + Name = order.Name, + Quantity = order.Quantity }; return result; @@ -59,7 +61,8 @@ public static OrderCreatedEvent ToCreatedEvent( var result = new OrderCreatedEvent { Id = order.Id, - Name = order.Name + Name = order.Name, + Quantity = order.Quantity }; return result; diff --git a/Microservice.Logic/Orders/Responses/OrderResponse.cs b/Microservice.Logic/Orders/Responses/OrderResponse.cs index 3180d64..c2ba918 100644 --- a/Microservice.Logic/Orders/Responses/OrderResponse.cs +++ b/Microservice.Logic/Orders/Responses/OrderResponse.cs @@ -4,5 +4,6 @@ public class OrderResponse { public long Id { get; set; } public string Name { get; set; } + public int Quantity { get; set; } } } From 8078eb235a62eeea3273827f5a67417c5d384937 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:53:12 +0200 Subject: [PATCH 16/37] fix event publisher wire ups --- .../EventPublishers/IOrderCreatedEventPublisher.cs | 10 ++++++++++ .../EventPublishers/IOrderUpdatedEventPublisher.cs | 10 ++++++++++ .../EventPublishers/OrderCreatedEventPublisher.cs | 2 +- .../EventPublishers/OrderUpdatedEventPublisher.cs | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 Microservice.Logic/Orders/EventPublishers/IOrderCreatedEventPublisher.cs create mode 100644 Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs diff --git a/Microservice.Logic/Orders/EventPublishers/IOrderCreatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/IOrderCreatedEventPublisher.cs new file mode 100644 index 0000000..91f5fe4 --- /dev/null +++ b/Microservice.Logic/Orders/EventPublishers/IOrderCreatedEventPublisher.cs @@ -0,0 +1,10 @@ +using Microservice.Logic.Orders.Events; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.EventPublishers +{ + public interface IOrderCreatedEventPublisher + { + Task Publish(OrderCreatedEvent eventModel); + } +} diff --git a/Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs new file mode 100644 index 0000000..9979b8f --- /dev/null +++ b/Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs @@ -0,0 +1,10 @@ +using Microservice.Logic.Orders.Events; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.EventPublishers +{ + public interface IOrderUpdatedEventPublisher + { + Task Publish(OrderUpdatedEvent eventModel); + } +} diff --git a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs index 7cee824..8d32e58 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs @@ -5,7 +5,7 @@ namespace Microservice.Logic.Orders.EventPublishers { - public class OrderCreatedEventPublisher : EventPublisher + public class OrderCreatedEventPublisher : EventPublisher,IOrderCreatedEventPublisher { private IRabbitMessageBrokerClient BrokerClient { get; } diff --git a/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs index 6473cba..0fb8df8 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs @@ -5,7 +5,7 @@ namespace Microservice.Logic.Orders.EventPublishers { - public class OrderUpdatedEventPublisher : EventPublisher + public class OrderUpdatedEventPublisher : EventPublisher,IOrderUpdatedEventPublisher { private IRabbitMessageBrokerClient BrokerClient { get; } From 7a15acf5c680b73ad6cb50655537e2b06f8c75be Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:53:31 +0200 Subject: [PATCH 17/37] Fix event publisher wireups --- Microservice.Api/Startup.cs | 17 +++++++++-------- .../Orders/Handlers/CreateOrderHandler.cs | 4 ++-- .../Orders/Handlers/PatchOrderHandler.cs | 4 ++-- .../Orders/Handlers/PutPlaceOrderHandler.cs | 19 ++++++++++++++----- ...BrokerSubscriptionsConfigurationBuilder.cs | 4 ++++ 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 6705ca4..8a1e596 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -5,11 +5,9 @@ using Microservice.Db.Configuration; using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; -using Microservice.Logic.Orders.Events; -using Microservice.Logic.Orders.SubscriptionHandlers; +using Microservice.Logic.Orders.EventPublishers; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; -using Microservice.RabbitMessageBrokerHelpers.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -46,11 +44,14 @@ public void ConfigureServices(IServiceCollection services) .AddMediatR(typeof(LogicServiceCollectionExtensions).Assembly) .AddMessageBroker(_configuration.GetSection("MessageBrokerSettings")) .AddBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) - .AddMessageBrokerSubscriptions(x => - x - .UsePool("Orders") - .Subscribe("OrderPlaced") - ) + .AddTransient() + .AddTransient() + + //.AddMessageBrokerSubscriptions(x => + // x + // .UsePool("Orders") + // .Subscribe("OrderPlaced") + //) ; } diff --git a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs index 169ae60..ef47053 100644 --- a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs @@ -12,9 +12,9 @@ namespace Microservice.Logic.Orders.Handlers public class CreateOrderHandler : IRequestHandler { private readonly MicroserviceDbContext _dbContext; - private readonly OrderCreatedEventPublisher _createdEventPublisher; + private readonly IOrderCreatedEventPublisher _createdEventPublisher; - public CreateOrderHandler(MicroserviceDbContext dbContext, OrderCreatedEventPublisher createdEventPublisher) + public CreateOrderHandler(MicroserviceDbContext dbContext, IOrderCreatedEventPublisher createdEventPublisher) { _dbContext = dbContext; _createdEventPublisher = createdEventPublisher; diff --git a/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs index 8ed6ca4..78a54f1 100644 --- a/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs @@ -14,9 +14,9 @@ namespace Microservice.Logic.Orders.Handlers public class PatchOrderHandler : IRequestHandler { private readonly MicroserviceDbContext _dbContext; - private readonly OrderUpdatedEventPublisher _updatedEventPublisher; + private readonly IOrderUpdatedEventPublisher _updatedEventPublisher; - public PatchOrderHandler(MicroserviceDbContext dbContext, OrderUpdatedEventPublisher updatedEventPublisher) + public PatchOrderHandler(MicroserviceDbContext dbContext, IOrderUpdatedEventPublisher updatedEventPublisher) { _dbContext = dbContext; _updatedEventPublisher = updatedEventPublisher; diff --git a/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs index a5ead58..936342a 100644 --- a/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs @@ -1,12 +1,14 @@ -using System.Threading; -using System.Threading.Tasks; +using System.Collections.Generic; using MediatR; using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.Queries; using Microservice.Logic.Orders.Responses; +using System.Threading; +using System.Threading.Tasks; namespace Microservice.Logic.Orders.Handlers { - public class PutPlaceOrderHandler: IRequestHandler + public class PutPlaceOrderHandler : IRequestHandler { private readonly IMediator _mediator; @@ -15,9 +17,16 @@ public PutPlaceOrderHandler(IMediator mediator) _mediator = mediator; } - public Task Handle(PutPlacedOrderCommand request, CancellationToken cancellationToken) + public async Task Handle(PutPlacedOrderCommand request, CancellationToken cancellationToken) { - throw new System.NotImplementedException(); + var order = await _mediator.Send(new GetOrderByIdQuery(request.Id), cancellationToken); + + if(order == null) + throw new KeyNotFoundException($"Unable to modify order because an entry with Id: {request.Id} could not be found"); + + return null; + + } } } diff --git a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs index a5db714..3eda3e7 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerSubscriptionsConfigurationBuilder.cs @@ -9,6 +9,10 @@ public class MessageBrokerSubscriptionsConfigurationBuilder public List Subscriptions; public string Pool; + public MessageBrokerSubscriptionsConfigurationBuilder() + { + Subscriptions = new List(); + } public MessageBrokerSubscriptionsConfigurationBuilder UsePool(string pool) { Pool = pool; From 8dfa8cd8df6d41b43b69957a17ede5215591933c Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:55:38 +0200 Subject: [PATCH 18/37] Move EventSubscription into EventSubscriptionHandlers --- Microservice.Api/Startup.cs | 14 ++++++++------ .../OrderPlacedEventSubscriptionHandler.cs} | 10 +++++----- 2 files changed, 13 insertions(+), 11 deletions(-) rename Microservice.Logic/Orders/{EventSubscriptions/OrderPlacedEventSubscription.cs => EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs} (56%) diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 8a1e596..ca82e7a 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -6,8 +6,11 @@ using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; using Microservice.Logic.Orders.EventPublishers; +using Microservice.Logic.Orders.Events; +using Microservice.Logic.Orders.EventSubscriptions; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; +using Microservice.RabbitMessageBrokerHelpers.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -46,12 +49,11 @@ public void ConfigureServices(IServiceCollection services) .AddBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) .AddTransient() .AddTransient() - - //.AddMessageBrokerSubscriptions(x => - // x - // .UsePool("Orders") - // .Subscribe("OrderPlaced") - //) + .AddMessageBrokerSubscriptions(x => + x + .UsePool("Orders") + .Subscribe("OrderPlaced") + ) ; } diff --git a/Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs b/Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs similarity index 56% rename from Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs rename to Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs index 0e74e51..ceba726 100644 --- a/Microservice.Logic/Orders/EventSubscriptions/OrderPlacedEventSubscription.cs +++ b/Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs @@ -1,15 +1,15 @@ -using MediatR; +using System.Threading.Tasks; +using MediatR; using Microservice.Logic.Orders.Events; using Microservice.RabbitMessageBrokerHelpers.Handlers; -using System.Threading.Tasks; -namespace Microservice.Logic.Orders.EventSubscriptions +namespace Microservice.Logic.Orders.EventSubscriptionHandlers { - public class OrderPlacedEventSubscription : MessageBrokerSubscriptionHandler + public class OrderPlacedEventSubscriptionHandler : MessageBrokerSubscriptionHandler { private readonly IMediator _mediator; - public OrderPlacedEventSubscription(IMediator mediator) + public OrderPlacedEventSubscriptionHandler(IMediator mediator) { _mediator = mediator; } From 0eeaedc6b85d2c6126f3f4cd1cc58b428756401a Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:58:54 +0200 Subject: [PATCH 19/37] Update OrderPlacedEventSubscriptionHandler.cs --- .../OrderPlacedEventSubscriptionHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs b/Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs index ceba726..65d508c 100644 --- a/Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs +++ b/Microservice.Logic/Orders/EventSubscriptionHandlers/OrderPlacedEventSubscriptionHandler.cs @@ -1,7 +1,7 @@ -using System.Threading.Tasks; -using MediatR; +using MediatR; using Microservice.Logic.Orders.Events; using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading.Tasks; namespace Microservice.Logic.Orders.EventSubscriptionHandlers { From 9527735433656ad3a3267a1546eb2034e44a3b22 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:58:59 +0200 Subject: [PATCH 20/37] Update Startup.cs --- Microservice.Api/Startup.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index ca82e7a..c08548b 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -7,7 +7,7 @@ using Microservice.Logic.Configuration; using Microservice.Logic.Orders.EventPublishers; using Microservice.Logic.Orders.Events; -using Microservice.Logic.Orders.EventSubscriptions; +using Microservice.Logic.Orders.EventSubscriptionHandlers; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; using Microservice.RabbitMessageBrokerHelpers.Configuration; @@ -52,7 +52,7 @@ public void ConfigureServices(IServiceCollection services) .AddMessageBrokerSubscriptions(x => x .UsePool("Orders") - .Subscribe("OrderPlaced") + .Subscribe("OrderPlaced") ) ; } From b0b50f43e3f26d45e35e7fd2441592ec28ee78a4 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 06:59:03 +0200 Subject: [PATCH 21/37] Update MessageBrokerSubscriptionBackgroundJob.cs --- .../BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs index 75694e9..400154f 100644 --- a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs +++ b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs @@ -28,14 +28,12 @@ public MessageBrokerSubscriptionBackgroundJob( IServiceProvider serviceProvider, IRabbitMessageBrokerClient messageBrokerClient, ILogger logger, - List unsubscribeCallbacks, IBackgroundProcessingClient backgroundProcessingClient) { _configurationBuilder = configurationBuilder; _serviceProvider = serviceProvider; _messageBrokerClient = messageBrokerClient; _logger = logger; - _unsubscribeCallbacks = unsubscribeCallbacks; _backgroundProcessingClient = backgroundProcessingClient; } From 1cfeaeaf40678cf096eae81ded003edd216f138f Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Mon, 4 May 2020 07:44:41 +0200 Subject: [PATCH 22/37] Refactor messagebroker pub sub wireups --- .../MessageBrokerCollectionExtensions.cs | 34 +++++++++++++++++++ Microservice.Api/Startup.cs | 13 +++---- ...undJobServerServiceCollectionExtensions.cs | 2 +- ...essageBrokerServiceCollectionExtensions.cs | 2 +- ...ageBrokerPublishersConfigurationbuilder.cs | 6 ++++ .../ServiceCollectionExtensions.cs | 15 ++++++++ 6 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs create mode 100644 Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs diff --git a/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs b/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs new file mode 100644 index 0000000..70b8419 --- /dev/null +++ b/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs @@ -0,0 +1,34 @@ +using Microservice.Logic.Orders.EventPublishers; +using Microservice.Logic.Orders.Events; +using Microservice.Logic.Orders.EventSubscriptionHandlers; +using Microservice.RabbitMessageBrokerHelpers.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Microservice.Api.Configuration +{ + public static class MessageBrokerCollectionExtensions + { + public static IServiceCollection AddMessageBrokerCustomSubscriptions(this IServiceCollection services) + { + services + .AddMessageBrokerSubscriptions(x => + x + .UsePool("Orders") + .Subscribe("OrderPlaced") + ) + ; + + return services; + } + + public static IServiceCollection AddMessageBrokerCustomPublishers(this IServiceCollection services) + { + services + .AddTransient() + .AddTransient() + ; + + return services; + } + } +} diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index c08548b..3ecd793 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -45,15 +45,10 @@ public void ConfigureServices(IServiceCollection services) .AddDatabase(_configuration.GetConnectionString("Database")) .AddLogic(_configuration) .AddMediatR(typeof(LogicServiceCollectionExtensions).Assembly) - .AddMessageBroker(_configuration.GetSection("MessageBrokerSettings")) - .AddBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) - .AddTransient() - .AddTransient() - .AddMessageBrokerSubscriptions(x => - x - .UsePool("Orders") - .Subscribe("OrderPlaced") - ) + .AddRabbitMqMessageBroker(_configuration.GetSection("MessageBrokerSettings")) + .AddHangfireBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) + .AddMessageBrokerCustomSubscriptions() + .AddMessageBrokerCustomPublishers() ; } diff --git a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs index fa2ac85..e631a54 100644 --- a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs +++ b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs @@ -12,7 +12,7 @@ namespace Microservice.HangfireBackgroundJobServer.Configuration { public static class BackgroundJobServerServiceCollectionExtensions { - public static IServiceCollection AddBackgroundJobServer(this IServiceCollection services, + public static IServiceCollection AddHangfireBackgroundJobServer(this IServiceCollection services, IConfiguration config) { var settings = new BackgroundJobServerSettings(); diff --git a/Microservice.RabbitMessageBroker/Configuration/MessageBrokerServiceCollectionExtensions.cs b/Microservice.RabbitMessageBroker/Configuration/MessageBrokerServiceCollectionExtensions.cs index 6d8d627..194d0b9 100644 --- a/Microservice.RabbitMessageBroker/Configuration/MessageBrokerServiceCollectionExtensions.cs +++ b/Microservice.RabbitMessageBroker/Configuration/MessageBrokerServiceCollectionExtensions.cs @@ -5,7 +5,7 @@ namespace Microservice.RabbitMessageBroker.Configuration { public static class MessageBrokerServiceCollectionExtensions { - public static IServiceCollection AddMessageBroker(this IServiceCollection container, IConfigurationSection configuration) + public static IServiceCollection AddRabbitMqMessageBroker(this IServiceCollection container, IConfigurationSection configuration) { container .Configure(configuration) diff --git a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs new file mode 100644 index 0000000..4daab3f --- /dev/null +++ b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs @@ -0,0 +1,6 @@ +namespace Microservice.RabbitMessageBrokerHelpers.Builders +{ + public class MessageBrokerPublishersConfigurationBuilder + { + } +} diff --git a/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs b/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs index 49761b8..76cab93 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Configuration/ServiceCollectionExtensions.cs @@ -21,5 +21,20 @@ public static IServiceCollection AddMessageBrokerSubscriptions(this IServiceColl .AddHostedService() ; } + + public static IServiceCollection AddMessageBrokerPublishers(this IServiceCollection services, + Action configure) + { + var configurationBuilder = new MessageBrokerSubscriptionsConfigurationBuilder(); + configure.Invoke(configurationBuilder); + + foreach (var subscription in configurationBuilder.Subscriptions) + services.AddTransient(subscription.Handler); + + return services + .AddSingleton(configurationBuilder) + .AddHostedService() + ; + } } } From 2fa72a4dfd2759d981ec6a4d8c25e9de442907eb Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 5 May 2020 08:31:39 +0200 Subject: [PATCH 23/37] Add microservice integratontests for events --- .../Microservice.Api.Integration.Tests.csproj | 12 +++ .../OrdersControllerTests.cs | 90 ++++++++++++++----- .../appsettings.json | 20 +++++ Microservice.Api/Startup.cs | 4 - .../Orders/Commands/CreateOrderCommand.cs | 6 +- .../Orders/Handlers/CreateOrderHandler.cs | 6 +- Microservice.Logic/Orders/OrderMapper.cs | 10 +++ .../RabbitMessageBusTests.cs | 5 +- .../RabbitMessageBrokerClient.cs | 2 + ...ageBrokerPublishersConfigurationbuilder.cs | 1 + 10 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 Microservice.Api.Integration.Tests/appsettings.json diff --git a/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj b/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj index b638fc7..917adee 100644 --- a/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj +++ b/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj @@ -8,6 +8,18 @@ 8 + + + + + + + Always + true + PreserveNewest + + + diff --git a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs index 90c3293..991dff7 100644 --- a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs +++ b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs @@ -2,14 +2,19 @@ using Microservice.Db; using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.Events; using Microservice.Logic.Orders.Responses; +using Microservice.RabbitMessageBroker; +using Microservice.RabbitMessageBroker.Configuration; using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using NUnit.Framework; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; @@ -22,10 +27,18 @@ public class OrdersControllerTests { private WebApplicationFactory _factory; private HttpClient _client; + private IRabbitMessageBrokerClient _messageBrokerClient; + private string PathBuilder(string extension) => $"api/{extension}"; + [OneTimeSetUp] public void Setup() { + var configBuilder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true); + var config = configBuilder.Build(); + _factory = new WebApplicationFactory() .WithWebHostBuilder(builder => { @@ -33,21 +46,22 @@ public void Setup() { services.RemoveAll(typeof(DbContextOptions)); services.RemoveAll(typeof(MicroserviceDbContext)); - services.AddDbContext(options => - { - options.UseInMemoryDatabase("TestInMemoryDatabase"); - }); + services.AddDbContext(); + services.AddRabbitMqMessageBroker(config.GetSection("RabbitMessageBrokerSettings")); }); }); _client = _factory.CreateClient(); + _messageBrokerClient = _factory.Services.GetService(); } [Test] - public async Task Given_CreateCustomerOrderCommand_Expect_OrderResponse() + public async Task Given_CreateOrderCommand_Which_Should_Publish_A_OrderCreatedEvent_Expect_OrderResponse_With_CreatedEvent_Published() { // Arrange - var createCustomerOrderCommand = new CreateOrderCommand("Testing command"); + const string topic = "OrderCreated"; + const string subscriptionId = "OrderCreated_IntegrationTest"; + var createCommand = new CreateOrderCommand("Keyboard", 5); _factory.Seed(db => { @@ -55,12 +69,30 @@ public async Task Given_CreateCustomerOrderCommand_Expect_OrderResponse() }); // Act - var response = await _client.PostAsJsonAsync(PathBuilder("orders"), createCustomerOrderCommand); + var response = await _client.PostAsJsonAsync(PathBuilder("orders"), createCommand); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created)); var result = response.Content.Deserialize().Result; - - // Assert - StringAssert.AreEqualIgnoringCase(createCustomerOrderCommand.Name, result.Name); + var subscriptionResponse = await _messageBrokerClient.Subscribe( + topic, + subscriptionId, + AssertCallback, + c => c.UseBasicQos()); + + // Assert Create Response + Assert.That(result.Quantity, Is.EqualTo(createCommand.Quantity)); + StringAssert.AreEqualIgnoringCase(createCommand.Name, result.Name); + + // Assert Messagebus OrderCreatedEvent + Task AssertCallback(OrderCreatedEvent orderCreatedEvent) + { + var tcs = new TaskCompletionSource(); + + StringAssert.AreEqualIgnoringCase(orderCreatedEvent.Name, result.Name); + Assert.That(orderCreatedEvent.Quantity, Is.EqualTo(createCommand.Quantity)); + Assert.That(orderCreatedEvent.Id, Is.GreaterThan(0)); + tcs.SetResult(orderCreatedEvent); + return tcs.Task; + } } [Test] @@ -126,10 +158,12 @@ public async Task Given_AllOrders_Expect_OrderResponses() } [Test] - public async Task Given_PatchOnOrder_Expact_Updated_OrderResponse() + public async Task Given_PatchOrder_Which_Should_Publish_OrderUpdatedEvent_Expect_Updated_OrderResponse_With_UpdatedEvent_Published() { // Arrange - var origionalOrder = new Order + const string topic = "Order"; + const string subscriptionId = "OrderUpdated"; + var originationOrder = new Order { Id = 1, Name = "product zero one" @@ -143,27 +177,35 @@ public async Task Given_PatchOnOrder_Expact_Updated_OrderResponse() _factory.Seed(db => { db.Clear(); - db.Orders.Add(origionalOrder); + db.Orders.Add(originationOrder); }); // Act - var response = await _client.PatchAsJsonAsync(PathBuilder($"orders/update/{origionalOrder.Id}/77"), patchDoc); + var response = await _client.PatchAsJsonAsync(PathBuilder($"orders/update/{originationOrder.Id}/77"), patchDoc); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); var result = response.Content.Deserialize().Result; + var subscriptionResponse = await _messageBrokerClient.Subscribe( + topic, + subscriptionId, + AssertCallback, + c => c.UseBasicQos()); - // Assert + // Assert Patch Operation StringAssert.AreEqualIgnoringCase($"PROD: zero one", result.Name); - } -// [Test] -// public async Task Given_CreateOrder_Publish_OrderCreatedEvent_Expect_OrderCreatedEvent_From_MessageBroker() -// { -// -// } + // Assert OrderUpdatedEvent + Task AssertCallback(OrderUpdatedEvent updatedEvent) + { + var tcs = new TaskCompletionSource(); + + StringAssert.AreEqualIgnoringCase($"PROD: zero one", updatedEvent.Name); + Assert.That(updatedEvent.Quantity, Is.EqualTo(result.Quantity)); + Assert.That(updatedEvent.Id, Is.EqualTo(result.Id)); + + tcs.SetResult(updatedEvent); + return tcs.Task; + } - private static string GetMessage() - { - return "info: Hello World!"; } } } diff --git a/Microservice.Api.Integration.Tests/appsettings.json b/Microservice.Api.Integration.Tests/appsettings.json new file mode 100644 index 0000000..082c4c1 --- /dev/null +++ b/Microservice.Api.Integration.Tests/appsettings.json @@ -0,0 +1,20 @@ +{ + "RabbitMessageBrokerSettings": { + "Host": "localhost", + "Port": 5672, + "UserName": "guest", + "Password": "guest", + "VirtualHost": "/", + "MaxConnectionRetries": 1000, + "ConnectionAttempBackoffFactor": 1.5, + "ConnectionAttempMaxBackoff": 10000, + "PublishConnectionTimeoutInSeconds": 10 + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 3ecd793..b5fa53d 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -5,12 +5,8 @@ using Microservice.Db.Configuration; using Microservice.HangfireBackgroundJobServer.Configuration; using Microservice.Logic.Configuration; -using Microservice.Logic.Orders.EventPublishers; -using Microservice.Logic.Orders.Events; -using Microservice.Logic.Orders.EventSubscriptionHandlers; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; -using Microservice.RabbitMessageBrokerHelpers.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; diff --git a/Microservice.Logic/Orders/Commands/CreateOrderCommand.cs b/Microservice.Logic/Orders/Commands/CreateOrderCommand.cs index 51556d7..66c6805 100644 --- a/Microservice.Logic/Orders/Commands/CreateOrderCommand.cs +++ b/Microservice.Logic/Orders/Commands/CreateOrderCommand.cs @@ -5,11 +5,13 @@ namespace Microservice.Logic.Orders.Commands { public class CreateOrderCommand : IRequest { - public string Name { get; set; } + public string Name { get; } + public int Quantity { get; } - public CreateOrderCommand(string name) + public CreateOrderCommand(string name, int quantity) { Name = name; + Quantity = quantity; } } } diff --git a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs index ef47053..bdabea1 100644 --- a/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/CreateOrderHandler.cs @@ -1,6 +1,5 @@ using MediatR; using Microservice.Db; -using Microservice.Db.EntityModels; using Microservice.Logic.Orders.Commands; using Microservice.Logic.Orders.EventPublishers; using Microservice.Logic.Orders.Responses; @@ -22,10 +21,7 @@ public CreateOrderHandler(MicroserviceDbContext dbContext, IOrderCreatedEventPub public async Task Handle(CreateOrderCommand request, CancellationToken cancellationToken) { - var entity = new Order - { - Name = request.Name - }; + var entity = request.ToCreateEntity(); _dbContext.Orders.Add(entity); await _dbContext.SaveChangesAsync(cancellationToken); diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 9752aa9..97a21b3 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -8,6 +8,16 @@ namespace Microservice.Logic.Orders { public static class OrderMapper { + public static Order ToCreateEntity(this CreateOrderCommand command) + { + var result = new Order + { + Name = command.Name, + Quantity = command.Quantity + }; + + return result; + } public static OrderResponse ToResponse(this Order order) { var result = new OrderResponse diff --git a/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs b/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs index 306fe7b..18504a8 100644 --- a/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs +++ b/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs @@ -23,8 +23,9 @@ public void SetUp() var services = new ServiceCollection(); services - .AddMessageBroker(config.GetSection("RabbitMessageBrokerSettings")) - .AddLogging(); + .AddRabbitMqMessageBroker(config.GetSection("RabbitMessageBrokerSettings")) + .AddLogging() + ; MessageBrokerClient = services.BuildServiceProvider().GetService(); } diff --git a/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs b/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs index e6bd3b5..20dd745 100644 --- a/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs +++ b/Microservice.RabbitMessageBroker/RabbitMessageBrokerClient.cs @@ -197,6 +197,8 @@ public async Task Subscribe(string topic, }; } + + public async Task Publish(string topic, T message) { var tcs = new TaskCompletionSource(); diff --git a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs index 4daab3f..f010049 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs +++ b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs @@ -2,5 +2,6 @@ { public class MessageBrokerPublishersConfigurationBuilder { + } } From 272d3d923cbe65cc46f050fec7c42480bdb14570 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Tue, 5 May 2020 10:53:57 +0200 Subject: [PATCH 24/37] WIP --- .../Infrastructure/HttpClientUtils.cs | 2 +- .../OrdersControllerTests.cs | 52 +++++++++++++++++++ .../MessageBrokerCollectionExtensions.cs | 3 +- .../Controllers/OrdersController.cs | 11 +++- .../Orders/Commands/PatchOrderCommand.cs | 3 +- .../Orders/Commands/PutOrderPlacedCommand.cs | 19 +++++++ .../Orders/Commands/PutPlacedOrderCommand.cs | 11 ---- ...sher.cs => IOrderPatchedEventPublisher.cs} | 2 +- .../IOrderPlacedEventPublisher.cs | 10 ++++ ...isher.cs => OrderPatchedEventPublisher.cs} | 4 +- .../OrderPlacedEventPublisher.cs | 20 +++++++ .../Orders/Events/OrderPlacedEvent.cs | 10 ++++ .../Events/OrderPlacedSubscriptionEvent.cs | 1 + .../Orders/Handlers/PatchOrderHandler.cs | 4 +- .../Orders/Handlers/PutOrderPlacedHandler.cs | 46 ++++++++++++++++ .../Orders/Handlers/PutPlaceOrderHandler.cs | 32 ------------ Microservice.Logic/Orders/OrderMapper.cs | 30 ++++++++--- 17 files changed, 200 insertions(+), 60 deletions(-) create mode 100644 Microservice.Logic/Orders/Commands/PutOrderPlacedCommand.cs delete mode 100644 Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs rename Microservice.Logic/Orders/EventPublishers/{IOrderUpdatedEventPublisher.cs => IOrderPatchedEventPublisher.cs} (79%) create mode 100644 Microservice.Logic/Orders/EventPublishers/IOrderPlacedEventPublisher.cs rename Microservice.Logic/Orders/EventPublishers/{OrderUpdatedEventPublisher.cs => OrderPatchedEventPublisher.cs} (75%) create mode 100644 Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs create mode 100644 Microservice.Logic/Orders/Events/OrderPlacedEvent.cs create mode 100644 Microservice.Logic/Orders/Handlers/PutOrderPlacedHandler.cs delete mode 100644 Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs diff --git a/Microservice.Api.Integration.Tests/Infrastructure/HttpClientUtils.cs b/Microservice.Api.Integration.Tests/Infrastructure/HttpClientUtils.cs index 961b2da..e57d85d 100644 --- a/Microservice.Api.Integration.Tests/Infrastructure/HttpClientUtils.cs +++ b/Microservice.Api.Integration.Tests/Infrastructure/HttpClientUtils.cs @@ -17,7 +17,7 @@ public static Task PostAsJsonAsync( return httpClient.PostAsync(url, content); } - public static Task PutAsJsonAsync( + public static Task PutAsJsonAsync( this HttpClient httpClient, string url, T data) { var dataAsString = JsonConvert.SerializeObject(data); diff --git a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs index 991dff7..bd58bfc 100644 --- a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs +++ b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs @@ -205,7 +205,59 @@ Task AssertCallback(OrderUpdatedEvent updatedEvent) tcs.SetResult(updatedEvent); return tcs.Task; } + } + + [Test] + public async Task Given_PutOrderPlacedCommand_Which_Should_Publish_A_PutOrderPlacedEvent_Expect_OrderResponse_With_OrderPlacedEvent_Published() + { + // Arrange + const string topic = "OrderPlacedUpdated"; + const string subscriptionId = "OrderPlacedUpdated_IntegrationTest"; + var command = new PutOrderPlacedCommand(1, 5, 2); + var originationOrder = new Order + { + Id = 1, + Name = "product zero one", + Quantity = 10 + }; + _factory.Seed(db => + { + db.Clear(); + db.Orders.Add(originationOrder); + }); + + // Act + var response = await _client.PutAsJsonAsync(PathBuilder($"orders/place"), command); + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); + var result = response.Content.Deserialize().Result; + + var subscriptionResponse = await _messageBrokerClient.Subscribe( + topic, + subscriptionId, + AssertCallback, + c => c.UseBasicQos()); + + // Assert PutOrderPlaced Response + Assert.That(result.Quantity, Is.EqualTo(8)); + Assert.That(result.Id, Is.EqualTo(originationOrder.Id)); + StringAssert.AreEqualIgnoringCase(originationOrder.Name, result.Name); + + + // Assert Messagebus OrderCreatedEvent + Task AssertCallback(OrderPlacedEvent orderPlacedEvent) + { + var tcs = new TaskCompletionSource(); + + Assert.That(orderPlacedEvent.QuantityBeforeReduction, Is.EqualTo(originationOrder.Quantity)); + Assert.That(orderPlacedEvent.Quantity, Is.EqualTo(result.Quantity)); + Assert.That(result.Id, Is.EqualTo(originationOrder.Id)); + StringAssert.AreEqualIgnoringCase(originationOrder.Name, result.Name); + + tcs.SetResult(orderPlacedEvent); + return tcs.Task; + } } + } } diff --git a/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs b/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs index 70b8419..4bf2b16 100644 --- a/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs +++ b/Microservice.Api/Configuration/MessageBrokerCollectionExtensions.cs @@ -25,7 +25,8 @@ public static IServiceCollection AddMessageBrokerCustomPublishers(this IServiceC { services .AddTransient() - .AddTransient() + .AddTransient() + .AddTransient() ; return services; diff --git a/Microservice.Api/Controllers/OrdersController.cs b/Microservice.Api/Controllers/OrdersController.cs index f6374b1..20eaa90 100644 --- a/Microservice.Api/Controllers/OrdersController.cs +++ b/Microservice.Api/Controllers/OrdersController.cs @@ -43,11 +43,18 @@ public async Task CreateOrder([FromBody] CreateOrderCommand comma } [HttpPatch("update/{id}/{personId}")] - public async Task PatchOrder([FromRoute]long id,[FromRoute] long personId, [FromBody] JsonPatchDocument patchModel) + public async Task PatchOrder([FromRoute]long id, [FromRoute] long personId, [FromBody] JsonPatchDocument patchModel) { - var command = new PatchOrderCommand(id,personId,patchModel); + var command = new PatchOrderCommand(id, personId, patchModel); var result = await _mediator.Send(command); return result != null ? (IActionResult)Ok(result) : NotFound(); } + + [HttpPut("place")] + public async Task PlaceOrder([FromBody]PutOrderPlacedCommand orderPlacedCommand) + { + var result = await _mediator.Send(orderPlacedCommand); + return result != null ? (IActionResult)Ok(result) : NotFound(); + } } } diff --git a/Microservice.Logic/Orders/Commands/PatchOrderCommand.cs b/Microservice.Logic/Orders/Commands/PatchOrderCommand.cs index 1fce294..1b12441 100644 --- a/Microservice.Logic/Orders/Commands/PatchOrderCommand.cs +++ b/Microservice.Logic/Orders/Commands/PatchOrderCommand.cs @@ -9,9 +9,10 @@ public class PatchOrderCommand : IRequest { public long OrderId { get; } public long PersonId { get; } + public JsonPatchDocument JsonPatchDocument { get; set; } - public PatchOrderCommand(long orderId, long personId,JsonPatchDocument jsonPatchDocument ) + public PatchOrderCommand(long orderId, long personId, JsonPatchDocument jsonPatchDocument) { OrderId = orderId; PersonId = personId; diff --git a/Microservice.Logic/Orders/Commands/PutOrderPlacedCommand.cs b/Microservice.Logic/Orders/Commands/PutOrderPlacedCommand.cs new file mode 100644 index 0000000..076007a --- /dev/null +++ b/Microservice.Logic/Orders/Commands/PutOrderPlacedCommand.cs @@ -0,0 +1,19 @@ +using MediatR; +using Microservice.Logic.Orders.Responses; + +namespace Microservice.Logic.Orders.Commands +{ + public class PutOrderPlacedCommand : IRequest + { + public long Id { get; } + public int Quantity { get; } + public long PersonId { get; } + + public PutOrderPlacedCommand(long id, long personId, int quantity) + { + PersonId = personId; + Quantity = quantity; + Id = id; + } + } +} diff --git a/Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs b/Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs deleted file mode 100644 index 583a452..0000000 --- a/Microservice.Logic/Orders/Commands/PutPlacedOrderCommand.cs +++ /dev/null @@ -1,11 +0,0 @@ -using MediatR; -using Microservice.Logic.Orders.Responses; - -namespace Microservice.Logic.Orders.Commands -{ - public class PutPlacedOrderCommand : IRequest - { - public long Id { get; set; } - public int Quantity { get; set; } - } -} diff --git a/Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/IOrderPatchedEventPublisher.cs similarity index 79% rename from Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs rename to Microservice.Logic/Orders/EventPublishers/IOrderPatchedEventPublisher.cs index 9979b8f..119d779 100644 --- a/Microservice.Logic/Orders/EventPublishers/IOrderUpdatedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/IOrderPatchedEventPublisher.cs @@ -3,7 +3,7 @@ namespace Microservice.Logic.Orders.EventPublishers { - public interface IOrderUpdatedEventPublisher + public interface IOrderPatchedEventPublisher { Task Publish(OrderUpdatedEvent eventModel); } diff --git a/Microservice.Logic/Orders/EventPublishers/IOrderPlacedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/IOrderPlacedEventPublisher.cs new file mode 100644 index 0000000..3fae5fe --- /dev/null +++ b/Microservice.Logic/Orders/EventPublishers/IOrderPlacedEventPublisher.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using Microservice.Logic.Orders.Events; + +namespace Microservice.Logic.Orders.EventPublishers +{ + public interface IOrderPlacedEventPublisher + { + Task Publish(OrderPlacedEvent orderPlacedEvent); + } +} diff --git a/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs similarity index 75% rename from Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs rename to Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs index 0fb8df8..4550ae6 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderUpdatedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs @@ -5,11 +5,11 @@ namespace Microservice.Logic.Orders.EventPublishers { - public class OrderUpdatedEventPublisher : EventPublisher,IOrderUpdatedEventPublisher + public class OrderPatchedEventPublisher : EventPublisher,IOrderPatchedEventPublisher { private IRabbitMessageBrokerClient BrokerClient { get; } - public OrderUpdatedEventPublisher(IRabbitMessageBrokerClient brokerClient) + public OrderPatchedEventPublisher(IRabbitMessageBrokerClient brokerClient) { BrokerClient = brokerClient; } diff --git a/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs new file mode 100644 index 0000000..fe6b87a --- /dev/null +++ b/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs @@ -0,0 +1,20 @@ +using Microservice.Logic.Orders.Events; +using Microservice.RabbitMessageBroker; +using Microservice.RabbitMessageBrokerHelpers.Handlers; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.EventPublishers +{ + public class OrderPlacedEventPublisher : EventPublisher, IOrderPlacedEventPublisher + { + private IRabbitMessageBrokerClient BrokerClient { get; } + + public OrderPlacedEventPublisher(IRabbitMessageBrokerClient brokerClient) + { + BrokerClient = brokerClient; + } + + public override async Task Publish(OrderPlacedEvent orderPlacedEvent) + => await BrokerClient.Publish("OrderPlacedUpdated", orderPlacedEvent); + } +} diff --git a/Microservice.Logic/Orders/Events/OrderPlacedEvent.cs b/Microservice.Logic/Orders/Events/OrderPlacedEvent.cs new file mode 100644 index 0000000..382d438 --- /dev/null +++ b/Microservice.Logic/Orders/Events/OrderPlacedEvent.cs @@ -0,0 +1,10 @@ +namespace Microservice.Logic.Orders.Events +{ + public class OrderPlacedEvent + { + public long Id { get; set; } + public string Name { get; set; } + public int Quantity { get; set; } + public int QuantityBeforeReduction { get; set; } + } +} diff --git a/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs b/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs index edcc391..7b7d605 100644 --- a/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs +++ b/Microservice.Logic/Orders/Events/OrderPlacedSubscriptionEvent.cs @@ -3,6 +3,7 @@ public class OrderPlacedSubscriptionEvent { public long Id { get; set; } + public long PersonId { get; set; } public int Quantity { get; set; } } } diff --git a/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs index 78a54f1..190d9e6 100644 --- a/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs +++ b/Microservice.Logic/Orders/Handlers/PatchOrderHandler.cs @@ -14,9 +14,9 @@ namespace Microservice.Logic.Orders.Handlers public class PatchOrderHandler : IRequestHandler { private readonly MicroserviceDbContext _dbContext; - private readonly IOrderUpdatedEventPublisher _updatedEventPublisher; + private readonly IOrderPatchedEventPublisher _updatedEventPublisher; - public PatchOrderHandler(MicroserviceDbContext dbContext, IOrderUpdatedEventPublisher updatedEventPublisher) + public PatchOrderHandler(MicroserviceDbContext dbContext, IOrderPatchedEventPublisher updatedEventPublisher) { _dbContext = dbContext; _updatedEventPublisher = updatedEventPublisher; diff --git a/Microservice.Logic/Orders/Handlers/PutOrderPlacedHandler.cs b/Microservice.Logic/Orders/Handlers/PutOrderPlacedHandler.cs new file mode 100644 index 0000000..7fdbc71 --- /dev/null +++ b/Microservice.Logic/Orders/Handlers/PutOrderPlacedHandler.cs @@ -0,0 +1,46 @@ +using MediatR; +using Microservice.Db; +using Microservice.Logic.Orders.Commands; +using Microservice.Logic.Orders.EventPublishers; +using Microservice.Logic.Orders.Responses; +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace Microservice.Logic.Orders.Handlers +{ + public class PutOrderPlacedHandler : IRequestHandler + { + private readonly MicroserviceDbContext _dbContext; + private readonly IOrderPlacedEventPublisher _orderPlacedEventPublisher; + + public PutOrderPlacedHandler(MicroserviceDbContext dbContext, IOrderPlacedEventPublisher orderPlacedEventPublisher) + { + _dbContext = dbContext; + _orderPlacedEventPublisher = orderPlacedEventPublisher; + } + + public async Task Handle(PutOrderPlacedCommand request, CancellationToken cancellationToken) + { + var order = await _dbContext.Orders.FindAsync(request.Id); + + if (order == null) + throw new KeyNotFoundException($"Unable to modify order because an entry with Id: {request.Id} could not be found"); + + if (order.Quantity < request.Quantity) + throw new ArgumentOutOfRangeException($"Unable to place order as the requested quantity ({request.Quantity}) is greater than the in stock quantity ({order.Quantity})"); + + var quantityBeforeReduction = order.Quantity; + order.Quantity -= request.Quantity; + + await _dbContext.SaveChangesAsync(cancellationToken); + + await _orderPlacedEventPublisher.Publish(order.ToOrderPlacedEvent(quantityBeforeReduction)); + + var response = order.ToResponse(); + + return response; + } + } +} diff --git a/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs b/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs deleted file mode 100644 index 936342a..0000000 --- a/Microservice.Logic/Orders/Handlers/PutPlaceOrderHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Collections.Generic; -using MediatR; -using Microservice.Logic.Orders.Commands; -using Microservice.Logic.Orders.Queries; -using Microservice.Logic.Orders.Responses; -using System.Threading; -using System.Threading.Tasks; - -namespace Microservice.Logic.Orders.Handlers -{ - public class PutPlaceOrderHandler : IRequestHandler - { - private readonly IMediator _mediator; - - public PutPlaceOrderHandler(IMediator mediator) - { - _mediator = mediator; - } - - public async Task Handle(PutPlacedOrderCommand request, CancellationToken cancellationToken) - { - var order = await _mediator.Send(new GetOrderByIdQuery(request.Id), cancellationToken); - - if(order == null) - throw new KeyNotFoundException($"Unable to modify order because an entry with Id: {request.Id} could not be found"); - - return null; - - - } - } -} diff --git a/Microservice.Logic/Orders/OrderMapper.cs b/Microservice.Logic/Orders/OrderMapper.cs index 97a21b3..58a0833 100644 --- a/Microservice.Logic/Orders/OrderMapper.cs +++ b/Microservice.Logic/Orders/OrderMapper.cs @@ -40,14 +40,15 @@ public static OrderPatchModel ToPatchModal(this Order order) return result; } - public static PutPlacedOrderCommand ToPutPlacedOrderCommand( + public static PutOrderPlacedCommand ToPutPlacedOrderCommand( this OrderPlacedSubscriptionEvent subscriptionEvent) { - var result = new PutPlacedOrderCommand - { - Id = subscriptionEvent.Id, - Quantity = subscriptionEvent.Quantity - }; + var result = new PutOrderPlacedCommand + ( + subscriptionEvent.Id, + subscriptionEvent.PersonId, + subscriptionEvent.Quantity + ); return result; } @@ -59,7 +60,22 @@ public static OrderUpdatedEvent ToUpdatedEvent( { Id = order.Id, Name = order.Name, - Quantity = order.Quantity + Quantity = order.Quantity, + }; + + return result; + } + + public static OrderPlacedEvent ToOrderPlacedEvent( + this Order order, + int quantityBeforeReduction) + { + var result = new OrderPlacedEvent + { + Id = order.Id, + Name = order.Name, + Quantity = order.Quantity, + QuantityBeforeReduction = quantityBeforeReduction }; return result; From 0b667c5f2bdb146cf3e70b5414e2719282c23cde Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Wed, 6 May 2020 06:39:06 +0200 Subject: [PATCH 25/37] refactor and clean up tests --- .../APIWebApplicationFactoryExtensions.cs | 30 +++++++- .../Infrastructure/BaseControllerTest.cs | 41 +++++++++++ .../BaseEventSubscriptionTest.cs | 37 ++++++++++ .../ConfigurationBuilderExtensions.cs | 27 +++++++ .../Microservice.Api.Integration.Tests.csproj | 2 + ...OrderPlacedEventSubscriptionHandlerTest.cs | 55 ++++++++++++++ .../OrdersControllerTests.cs | 73 ++++++------------- 7 files changed, 214 insertions(+), 51 deletions(-) create mode 100644 Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs create mode 100644 Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs create mode 100644 Microservice.Api.Integration.Tests/Infrastructure/ConfigurationBuilderExtensions.cs create mode 100644 Microservice.Api.Integration.Tests/OrderPlacedEventSubscriptionHandlerTest.cs diff --git a/Microservice.Api.Integration.Tests/Infrastructure/APIWebApplicationFactoryExtensions.cs b/Microservice.Api.Integration.Tests/Infrastructure/APIWebApplicationFactoryExtensions.cs index d366fb8..8dae275 100644 --- a/Microservice.Api.Integration.Tests/Infrastructure/APIWebApplicationFactoryExtensions.cs +++ b/Microservice.Api.Integration.Tests/Infrastructure/APIWebApplicationFactoryExtensions.cs @@ -1,12 +1,30 @@ -using System; -using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting.Internal; +using System; +using System.Collections.Generic; +using System.IO; namespace Microservice.Api.Integration.Tests.Infrastructure { public static class APIWebApplicationFactoryExtensions { + public static WebApplicationFactory WithAppSettings( + this WebApplicationFactory factory, + IEnumerable> customSettings = null) + where TStartup : class + => factory.WithWebHostBuilder(x => + { + var projDir = Directory.GetCurrentDirectory(); + var configpath = Path.Combine(projDir, "appsettings.json"); + x.ConfigureServices(c => c.AddSingleton(new HostingEnvironment {EnvironmentName = "Testing"})); + x.ConfigureAppConfiguration((c, d) => d.AddJsonFile(configpath)); + if (customSettings != null) + x.ConfigureAppConfiguration((c, d) => d.AddInMemoryCollection(customSettings)); + }); + public static WebApplicationFactory Seed( this WebApplicationFactory factory, Action seed) @@ -22,5 +40,13 @@ public static WebApplicationFactory Seed( } return factory; } + + public static TService GetScopedService( + this WebApplicationFactory factory) + where TStartup : class + => factory.Server.Services.CreateScope().ServiceProvider.GetService(); + + + } } diff --git a/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs b/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs new file mode 100644 index 0000000..08137f6 --- /dev/null +++ b/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs @@ -0,0 +1,41 @@ +using Microservice.Db; +using Microservice.RabbitMessageBroker; +using Microservice.RabbitMessageBroker.Configuration; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using NUnit.Framework; +using System.Net.Http; + +namespace Microservice.Api.Integration.Tests.Infrastructure +{ + public class BaseControllerTest + { + protected internal WebApplicationFactory Factory; + protected internal HttpClient Client; + protected internal IRabbitMessageBrokerClient MessageBrokerClient; + public string BaseRoute { get; set; } + + [OneTimeSetUp] + public void Setup() + { + Factory = new WebApplicationFactory() + .WithWebHostBuilder(builder => + { + builder.ConfigureServices(services => + { + services.RemoveAll(typeof(DbContextOptions)); + services.RemoveAll(typeof(MicroserviceDbContext)); + services.AddDbContext(); + services.AddRabbitMqMessageBroker(ConfigurationBuilderExtensions.GetCustomSection("RabbitMessageBrokerSettings")); + }); + }); + + Client = Factory.CreateClient(); + MessageBrokerClient = Factory.Services.GetService(); + } + + public string GetBaseRoute() => $"api/{BaseRoute}"; + } +} diff --git a/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs b/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs new file mode 100644 index 0000000..3d85f6d --- /dev/null +++ b/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs @@ -0,0 +1,37 @@ +using Microservice.RabbitMessageBroker; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Moq; +using NUnit.Framework; + +namespace Microservice.Api.Integration.Tests.Infrastructure +{ + public class BaseEventSubscriptionTest + { + private MockRepository _mockRepository; + protected internal Mock MessageBrokerClient; + protected internal WebApplicationFactory Factory; + + [OneTimeSetUp] + public void Setup() + { + _mockRepository = new MockRepository(MockBehavior.Strict); + MessageBrokerClient = _mockRepository.Create(MockBehavior.Loose); + Factory = new WebApplicationFactory() + + .WithWebHostBuilder(x => + { + x.ConfigureAppConfiguration((c, b) => { b.AddConfiguration(ConfigurationBuilderExtensions.GetConfigurationRoot()); }); + x.ConfigureTestServices(s => { s.AddTransient(_ => MessageBrokerClient.Object); }); + }) + ; + } + [TearDown] + public void TearDown() + { + _mockRepository.VerifyAll(); + } + } +} diff --git a/Microservice.Api.Integration.Tests/Infrastructure/ConfigurationBuilderExtensions.cs b/Microservice.Api.Integration.Tests/Infrastructure/ConfigurationBuilderExtensions.cs new file mode 100644 index 0000000..e160a40 --- /dev/null +++ b/Microservice.Api.Integration.Tests/Infrastructure/ConfigurationBuilderExtensions.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Configuration; +using System.IO; + +namespace Microservice.Api.Integration.Tests.Infrastructure +{ + public static class ConfigurationBuilderExtensions + { + public static IConfigurationSection GetCustomSection(string section) + { + var configBuilder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true); + var config = configBuilder.Build(); + + return config.GetSection(section); + } + + public static IConfigurationRoot GetConfigurationRoot() + { + var configBuilder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true); + + return configBuilder.Build(); + } + } +} diff --git a/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj b/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj index 917adee..65af76a 100644 --- a/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj +++ b/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj @@ -24,6 +24,7 @@ + @@ -31,6 +32,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + all diff --git a/Microservice.Api.Integration.Tests/OrderPlacedEventSubscriptionHandlerTest.cs b/Microservice.Api.Integration.Tests/OrderPlacedEventSubscriptionHandlerTest.cs new file mode 100644 index 0000000..68bcfea --- /dev/null +++ b/Microservice.Api.Integration.Tests/OrderPlacedEventSubscriptionHandlerTest.cs @@ -0,0 +1,55 @@ +using Microservice.Api.Integration.Tests.Infrastructure; +using Microservice.Db; +using Microservice.Db.EntityModels; +using Microservice.Logic.Orders.Events; +using NUnit.Framework; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace Microservice.Api.Integration.Tests +{ + [TestFixture] + public class OrderPlacedEventSubscriptionHandlerTest: BaseEventSubscriptionTest + { + [Test] + public async Task Given_OrderPlacedSubscriptionEvent_Expect_OrderResponse_With_OrderPlacedEvent_Published() + { + // Arrange + var order = new Order + { + Id = 2, + Name = "product zero two", + Quantity = 5 + }; + + var orderPlacedSubscriptionEvent = new OrderPlacedSubscriptionEvent + { + Id = order.Id, + Quantity = 2, + PersonId = 33 + }; + + Factory.Seed(db => + { + db.Clear(); + db.Orders.Add(order); + }); + + // Act + var handler = MessageBrokerClient.Invocations. + First(i => (string)i.Arguments[0] == "OrderPlaced") + .Arguments[2] as Func; + + await handler.Invoke(orderPlacedSubscriptionEvent); + + var dbContext = Factory.GetScopedService(); + + var orderResult = dbContext.Orders.Find(order.Id); + + // Assert handler + Assert.That(orderResult, Is.Not.Null); + Assert.That(orderResult.Quantity, Is.EqualTo(order.Quantity - orderPlacedSubscriptionEvent.Quantity)); + } + } +} diff --git a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs index bd58bfc..43131fb 100644 --- a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs +++ b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs @@ -23,36 +23,11 @@ namespace Microservice.Api.Integration.Tests { [TestFixture] - public class OrdersControllerTests + public class OrdersControllerTests : BaseControllerTest { - private WebApplicationFactory _factory; - private HttpClient _client; - private IRabbitMessageBrokerClient _messageBrokerClient; - - private string PathBuilder(string extension) => $"api/{extension}"; - - [OneTimeSetUp] - public void Setup() + public OrdersControllerTests() { - var configBuilder = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: true); - var config = configBuilder.Build(); - - _factory = new WebApplicationFactory() - .WithWebHostBuilder(builder => - { - builder.ConfigureServices(services => - { - services.RemoveAll(typeof(DbContextOptions)); - services.RemoveAll(typeof(MicroserviceDbContext)); - services.AddDbContext(); - services.AddRabbitMqMessageBroker(config.GetSection("RabbitMessageBrokerSettings")); - }); - }); - - _client = _factory.CreateClient(); - _messageBrokerClient = _factory.Services.GetService(); + BaseRoute = "orders"; } [Test] @@ -63,16 +38,16 @@ public async Task Given_CreateOrderCommand_Which_Should_Publish_A_OrderCreatedEv const string subscriptionId = "OrderCreated_IntegrationTest"; var createCommand = new CreateOrderCommand("Keyboard", 5); - _factory.Seed(db => + Factory.Seed(db => { db.Clear(); }); // Act - var response = await _client.PostAsJsonAsync(PathBuilder("orders"), createCommand); + var response = await Client.PostAsJsonAsync(GetBaseRoute(), createCommand); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created)); var result = response.Content.Deserialize().Result; - var subscriptionResponse = await _messageBrokerClient.Subscribe( + var subscriptionResponse = await MessageBrokerClient.Subscribe( topic, subscriptionId, AssertCallback, @@ -105,14 +80,14 @@ public async Task Given_GetOrderById_Expect_OrderResponse() Name = "Testing command" }; - _factory.Seed(db => + Factory.Seed(db => { db.Clear(); db.Orders.Add(order); }); // Act - var response = await _client.GetAsync(PathBuilder($"orders/{order.Id}")); + var response = await Client.GetAsync($"{GetBaseRoute()}/{order.Id}"); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); var result = response.Content.Deserialize().Result; @@ -143,13 +118,13 @@ public async Task Given_AllOrders_Expect_OrderResponses() Name = "Testing command" }; - _factory.Seed(db => + Factory.Seed(db => { db.Clear(); db.Orders.AddRange(order1, order2, order3); }); - var response = await _client.GetAsync(PathBuilder($"orders")); + var response = await Client.GetAsync($"{GetBaseRoute()}"); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); var result = response.Content.Deserialize>().Result; @@ -174,17 +149,17 @@ public async Task Given_PatchOrder_Which_Should_Publish_OrderUpdatedEvent_Expect var operations = patchDoc.Operations.ToList(); - _factory.Seed(db => + Factory.Seed(db => { db.Clear(); db.Orders.Add(originationOrder); }); // Act - var response = await _client.PatchAsJsonAsync(PathBuilder($"orders/update/{originationOrder.Id}/77"), patchDoc); + var response = await Client.PatchAsJsonAsync($"{GetBaseRoute()}/update/{originationOrder.Id}/77", patchDoc); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); var result = response.Content.Deserialize().Result; - var subscriptionResponse = await _messageBrokerClient.Subscribe( + var subscriptionResponse = await MessageBrokerClient.Subscribe( topic, subscriptionId, AssertCallback, @@ -214,25 +189,25 @@ public async Task Given_PutOrderPlacedCommand_Which_Should_Publish_A_PutOrderPla const string topic = "OrderPlacedUpdated"; const string subscriptionId = "OrderPlacedUpdated_IntegrationTest"; var command = new PutOrderPlacedCommand(1, 5, 2); - var originationOrder = new Order + var order = new Order { Id = 1, Name = "product zero one", Quantity = 10 }; - _factory.Seed(db => + Factory.Seed(db => { db.Clear(); - db.Orders.Add(originationOrder); + db.Orders.Add(order); }); // Act - var response = await _client.PutAsJsonAsync(PathBuilder($"orders/place"), command); + var response = await Client.PutAsJsonAsync($"{GetBaseRoute()}/place", command); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); var result = response.Content.Deserialize().Result; - var subscriptionResponse = await _messageBrokerClient.Subscribe( + var subscriptionResponse = await MessageBrokerClient.Subscribe( topic, subscriptionId, AssertCallback, @@ -240,24 +215,24 @@ public async Task Given_PutOrderPlacedCommand_Which_Should_Publish_A_PutOrderPla // Assert PutOrderPlaced Response Assert.That(result.Quantity, Is.EqualTo(8)); - Assert.That(result.Id, Is.EqualTo(originationOrder.Id)); - StringAssert.AreEqualIgnoringCase(originationOrder.Name, result.Name); - + Assert.That(result.Id, Is.EqualTo(order.Id)); + StringAssert.AreEqualIgnoringCase(order.Name, result.Name); // Assert Messagebus OrderCreatedEvent Task AssertCallback(OrderPlacedEvent orderPlacedEvent) { var tcs = new TaskCompletionSource(); - Assert.That(orderPlacedEvent.QuantityBeforeReduction, Is.EqualTo(originationOrder.Quantity)); + Assert.That(orderPlacedEvent.QuantityBeforeReduction, Is.EqualTo(order.Quantity)); Assert.That(orderPlacedEvent.Quantity, Is.EqualTo(result.Quantity)); - Assert.That(result.Id, Is.EqualTo(originationOrder.Id)); - StringAssert.AreEqualIgnoringCase(originationOrder.Name, result.Name); + Assert.That(result.Id, Is.EqualTo(order.Id)); + StringAssert.AreEqualIgnoringCase(order.Name, result.Name); tcs.SetResult(orderPlacedEvent); return tcs.Task; } } + } } From 26eea6e6a1d572f036d6490e8f949c8ddc5fe13b Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Wed, 6 May 2020 06:41:02 +0200 Subject: [PATCH 26/37] Update OrdersControllerTests.cs --- .../OrdersControllerTests.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs index 43131fb..3cd9964 100644 --- a/Microservice.Api.Integration.Tests/OrdersControllerTests.cs +++ b/Microservice.Api.Integration.Tests/OrdersControllerTests.cs @@ -4,20 +4,11 @@ using Microservice.Logic.Orders.Commands; using Microservice.Logic.Orders.Events; using Microservice.Logic.Orders.Responses; -using Microservice.RabbitMessageBroker; -using Microservice.RabbitMessageBroker.Configuration; using Microsoft.AspNetCore.JsonPatch; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using NUnit.Framework; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Net; -using System.Net.Http; using System.Threading.Tasks; namespace Microservice.Api.Integration.Tests From 5ac0e31057e6fcddd27b097aa59aa73c966854b2 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Wed, 6 May 2020 13:31:27 +0200 Subject: [PATCH 27/37] Addition of background processing --- Microservice.Api/Startup.cs | 2 + Microservice.Api/appsettings.json | 10 +++++ ...undJobServerServiceCollectionExtensions.cs | 7 ++- .../Extensions/JobConfig.cs | 8 ++++ .../Extensions/RecurringJobConfig.cs | 8 ++++ .../BackgroundProcessingClient.cs | 8 ++-- ...ndProcessingServiceCollectionExtensions.cs | 20 +++++++++ .../HeartbeatRecurringJob.cs | 33 ++++++++++++++ .../BackgroundProcessing/JobScheduler.cs | 44 +++++++++++++++++++ .../BackgroundProcessing/JobSettings.cs | 12 +++++ .../OrderPlacedEventSubscriptionJob.cs | 10 +++++ .../ReStockZeroQuantityItemsJobConfig.cs | 9 ++++ .../ReStockZeroQuantityItemsRecurringJob.cs | 24 ++++++++++ 13 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 Microservice.HangfireBackgroundJobServer/Extensions/JobConfig.cs create mode 100644 Microservice.HangfireBackgroundJobServer/Extensions/RecurringJobConfig.cs create mode 100644 Microservice.Logic/BackgroundProcessing/BackgroundProcessingServiceCollectionExtensions.cs create mode 100644 Microservice.Logic/BackgroundProcessing/HeartbeatRecurringJob.cs create mode 100644 Microservice.Logic/BackgroundProcessing/JobScheduler.cs create mode 100644 Microservice.Logic/BackgroundProcessing/JobSettings.cs create mode 100644 Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs create mode 100644 Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsJobConfig.cs create mode 100644 Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsRecurringJob.cs diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index b5fa53d..21af1bc 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -4,6 +4,7 @@ using Microservice.Api.Filters; using Microservice.Db.Configuration; using Microservice.HangfireBackgroundJobServer.Configuration; +using Microservice.Logic.BackgroundProcessing; using Microservice.Logic.Configuration; using Microservice.Logic.Orders.Validators; using Microservice.RabbitMessageBroker.Configuration; @@ -45,6 +46,7 @@ public void ConfigureServices(IServiceCollection services) .AddHangfireBackgroundJobServer(_configuration.GetSection("BackgroundJobServerSettings")) .AddMessageBrokerCustomSubscriptions() .AddMessageBrokerCustomPublishers() + .AddBackgroundProcessing(_configuration) ; } diff --git a/Microservice.Api/appsettings.json b/Microservice.Api/appsettings.json index 06806d0..43d7449 100644 --- a/Microservice.Api/appsettings.json +++ b/Microservice.Api/appsettings.json @@ -16,6 +16,16 @@ "BackgroundJobServerSettings": { "ConnectionString": "Host=localhost;Username=postgres;Password=root;Database=microservice.db;Port=5433" }, + "BackgroundProcessing": { + "Heartbeat": { + "Enabled": true, + "Cron": "* * * * *" + }, + "ReStockZeroQuantityItems": { + "Enabled": true, + "Cron": "* * */1 * *" + } + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs index e631a54..f4dc20b 100644 --- a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs +++ b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using Hangfire; +using Hangfire; using Hangfire.Dashboard; using Hangfire.PostgreSql; using Microservice.HangfireBackgroundJobServer.Infrastructure; @@ -7,6 +6,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using System.Collections.Generic; namespace Microservice.HangfireBackgroundJobServer.Configuration { @@ -22,10 +22,13 @@ public static IServiceCollection AddHangfireBackgroundJobServer(this IServiceCol .Configure(config) ; + JobStorage.Current = new PostgreSqlStorage(settings.ConnectionString); + if (!string.IsNullOrEmpty(settings.ConnectionString)) { services .AddHangfire(o => o + .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() diff --git a/Microservice.HangfireBackgroundJobServer/Extensions/JobConfig.cs b/Microservice.HangfireBackgroundJobServer/Extensions/JobConfig.cs new file mode 100644 index 0000000..5352de0 --- /dev/null +++ b/Microservice.HangfireBackgroundJobServer/Extensions/JobConfig.cs @@ -0,0 +1,8 @@ +namespace Microservice.HangfireBackgroundJobServer.Extensions +{ + public class JobConfig + { + public string Cron { get; set; } + public bool Enabled { get; set; } + } +} diff --git a/Microservice.HangfireBackgroundJobServer/Extensions/RecurringJobConfig.cs b/Microservice.HangfireBackgroundJobServer/Extensions/RecurringJobConfig.cs new file mode 100644 index 0000000..142619b --- /dev/null +++ b/Microservice.HangfireBackgroundJobServer/Extensions/RecurringJobConfig.cs @@ -0,0 +1,8 @@ +namespace Microservice.HangfireBackgroundJobServer.Extensions +{ + public class RecurringJobConfig + { + public bool Enabled { get; set; } + public string Cron { get; set; } + } +} diff --git a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs index d75624c..1e4cab4 100644 --- a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs +++ b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs @@ -1,8 +1,9 @@ -using System; +using Hangfire; +using Hangfire.PostgreSql; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Linq.Expressions; using System.Threading.Tasks; -using Hangfire; -using Microsoft.Extensions.DependencyInjection; namespace Microservice.HangfireBackgroundJobServer.Infrastructure { @@ -56,7 +57,6 @@ public void ConfigureRecurringJob( if (jobRunner == null) throw new Exception("Could not activate recurring job. Ensure it is configured on the service provider"); - if (enabled) RecurringJob.AddOrUpdate( recurringJobId, diff --git a/Microservice.Logic/BackgroundProcessing/BackgroundProcessingServiceCollectionExtensions.cs b/Microservice.Logic/BackgroundProcessing/BackgroundProcessingServiceCollectionExtensions.cs new file mode 100644 index 0000000..514b6de --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/BackgroundProcessingServiceCollectionExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Microservice.Logic.BackgroundProcessing +{ + public static class BackgroundProcessingServiceCollectionExtensions + { + public static IServiceCollection AddBackgroundProcessing(this IServiceCollection container, IConfiguration config) + { + container + .Configure(config.GetSection("BackgroundProcessing")) + .AddHostedService() + .AddTransient() + .AddTransient() + ; + + return container; + } + } +} diff --git a/Microservice.Logic/BackgroundProcessing/HeartbeatRecurringJob.cs b/Microservice.Logic/BackgroundProcessing/HeartbeatRecurringJob.cs new file mode 100644 index 0000000..2312d72 --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/HeartbeatRecurringJob.cs @@ -0,0 +1,33 @@ +using Hangfire; +using Microservice.HangfireBackgroundJobServer; +using Microsoft.Extensions.Logging; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace Microservice.Logic.BackgroundProcessing +{ + public class HeartbeatRecurringJob : IRecurringJob + { + private readonly ILogger _logger; + public HeartbeatRecurringJob(ILogger logger) + { + _logger = logger; + } + + public Task Run(IJobCancellationToken cancellationToken) + { + var sw = new Stopwatch(); + sw.Start(); + + _logger.LogInformation("Heartbeat completed, {@details}", new + { + HeartbeatCompleted = new + { + Took = sw.ElapsedMilliseconds + } + }); + + return Task.CompletedTask; + } + } +} diff --git a/Microservice.Logic/BackgroundProcessing/JobScheduler.cs b/Microservice.Logic/BackgroundProcessing/JobScheduler.cs new file mode 100644 index 0000000..abb6996 --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/JobScheduler.cs @@ -0,0 +1,44 @@ +using Microservice.HangfireBackgroundJobServer.Infrastructure; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using System.Threading; +using System.Threading.Tasks; + +namespace Microservice.Logic.BackgroundProcessing +{ + public class JobScheduler : IHostedService + { + private readonly IBackgroundProcessingClient _backgroundProcessingClient; + private readonly IOptionsMonitor _settings; + + public JobScheduler( + IBackgroundProcessingClient backgroundProcessingClient, + IOptionsMonitor settings) + { + _backgroundProcessingClient = backgroundProcessingClient; + _settings = settings; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _backgroundProcessingClient.ConfigureRecurringJob( + "Heartbeat", + _settings.CurrentValue.Heartbeat.Cron, + _settings.CurrentValue.Heartbeat.Enabled + ); + + _backgroundProcessingClient.ConfigureRecurringJob( + "ReStockZeroQuantityItems", + _settings.CurrentValue.ReStockZeroQuantityItems.Cron, + _settings.CurrentValue.ReStockZeroQuantityItems.Enabled + ); + + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +} diff --git a/Microservice.Logic/BackgroundProcessing/JobSettings.cs b/Microservice.Logic/BackgroundProcessing/JobSettings.cs new file mode 100644 index 0000000..085a51b --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/JobSettings.cs @@ -0,0 +1,12 @@ +using Microservice.HangfireBackgroundJobServer.Extensions; + +namespace Microservice.Logic.BackgroundProcessing +{ + public class JobSettings + { + public RecurringJobConfig Heartbeat { get; set; } + public RecurringJobConfig CapStockItemsToFixedQuantityRecurringJob { get; set; } + + public ReStockZeroQuantityItemsJobConfig ReStockZeroQuantityItems { get; set; } + } +} diff --git a/Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs b/Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs new file mode 100644 index 0000000..c0f7c22 --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs @@ -0,0 +1,10 @@ +namespace Microservice.Logic.BackgroundProcessing +{ + public class OrderPlacedEventSubscriptionJob + { + public OrderPlacedEventSubscriptionJob() + { + + } + } +} diff --git a/Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsJobConfig.cs b/Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsJobConfig.cs new file mode 100644 index 0000000..1edfcde --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsJobConfig.cs @@ -0,0 +1,9 @@ +using Microservice.HangfireBackgroundJobServer.Extensions; + +namespace Microservice.Logic.BackgroundProcessing +{ + public class ReStockZeroQuantityItemsJobConfig : RecurringJobConfig + { + public int ZeroQuantityLimit { get; set; } + } +} \ No newline at end of file diff --git a/Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsRecurringJob.cs b/Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsRecurringJob.cs new file mode 100644 index 0000000..238ff9f --- /dev/null +++ b/Microservice.Logic/BackgroundProcessing/ReStockZeroQuantityItemsRecurringJob.cs @@ -0,0 +1,24 @@ +using Hangfire; +using MediatR; +using Microservice.HangfireBackgroundJobServer; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; + +namespace Microservice.Logic.BackgroundProcessing +{ + public class ReStockZeroQuantityItemsRecurringJob : IRecurringJob + { + private readonly ILogger _logger; + private readonly IMediator _mediator; + public ReStockZeroQuantityItemsRecurringJob(ILogger logger, IMediator mediator) + { + _logger = logger; + _mediator = mediator; + } + + public Task Run(IJobCancellationToken cancellationToken) + { + return Task.CompletedTask; + } + } +} From 9f6ff36b37c287383064b9e7656d1371fc556650 Mon Sep 17 00:00:00 2001 From: Armand Jordaan Date: Wed, 6 May 2020 13:44:19 +0200 Subject: [PATCH 28/37] Update dotnetcore.yml --- .github/workflows/dotnetcore.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 6d2a7d6..4b09db6 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -27,7 +27,7 @@ jobs: - name: Build run: dotnet build --configuration Release --no-restore - name: Install dotnet-ef tool - run: dotnet tool install --global dotnet-ef --version 3.1.0 + run: dotnet tool install --global dotnet-ef - name: Apply Migrations run: dotnet ef database update -s Microservice.Api/Microservice.Api.csproj -p Microservice.Db/Microservice.Db.csproj -c MicroserviceDbContext - name: Test From c7cdf6e5b840edf84151d0a2b08bc50724e5448e Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 9 May 2020 16:50:47 +0200 Subject: [PATCH 29/37] Update JobScheduler.cs --- Microservice.Logic/BackgroundProcessing/JobScheduler.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Microservice.Logic/BackgroundProcessing/JobScheduler.cs b/Microservice.Logic/BackgroundProcessing/JobScheduler.cs index abb6996..7b777b5 100644 --- a/Microservice.Logic/BackgroundProcessing/JobScheduler.cs +++ b/Microservice.Logic/BackgroundProcessing/JobScheduler.cs @@ -21,6 +21,7 @@ public JobScheduler( public Task StartAsync(CancellationToken cancellationToken) { + _backgroundProcessingClient.ConfigureRecurringJob( "Heartbeat", _settings.CurrentValue.Heartbeat.Cron, From 566b25977fe01c6298d8d25fa83913c77d97cab7 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 9 May 2020 16:50:55 +0200 Subject: [PATCH 30/37] Update IBackgroundProcessingClient.cs --- .../Infrastructure/IBackgroundProcessingClient.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs b/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs index d39bcee..41fc741 100644 --- a/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs +++ b/Microservice.HangfireBackgroundJobServer/Infrastructure/IBackgroundProcessingClient.cs @@ -11,7 +11,10 @@ public interface IBackgroundProcessingClient Task Run(Expression> action); void RemoveRecurringJobIfExists(string recurringJobId); - void ConfigureRecurringJob(string recurringJobId, string cronExpression, bool enabled = true) + void ConfigureRecurringJob( + string recurringJobId, + string cronExpression, + bool enabled = true) where T : IRecurringJob; } } From fdf33b5912a96f59e151e1b298b3a0896b738612 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 9 May 2020 16:51:07 +0200 Subject: [PATCH 31/37] Storage wire up fix --- .../Infrastructure/BackgroundProcessingClient.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs index 1e4cab4..c18c8ca 100644 --- a/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs +++ b/Microservice.HangfireBackgroundJobServer/Infrastructure/BackgroundProcessingClient.cs @@ -1,19 +1,23 @@ using Hangfire; -using Hangfire.PostgreSql; +using Microservice.HangfireBackgroundJobServer.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using System; using System.Linq.Expressions; using System.Threading.Tasks; +using Hangfire.PostgreSql; namespace Microservice.HangfireBackgroundJobServer.Infrastructure { public class BackgroundProcessingClient : IBackgroundProcessingClient { private readonly IServiceProvider _serviceProvider; + private readonly IOptions _options; - public BackgroundProcessingClient(IServiceProvider serviceProvider) + public BackgroundProcessingClient(IServiceProvider serviceProvider, IOptions options) { _serviceProvider = serviceProvider; + _options = options; } public string Enqueue(Expression action) @@ -54,9 +58,11 @@ public void ConfigureRecurringJob( where T : IRecurringJob { var jobRunner = _serviceProvider.CreateScope().ServiceProvider.GetService(); + JobStorage.Current = new PostgreSqlStorage(_options.Value.ConnectionString); if (jobRunner == null) throw new Exception("Could not activate recurring job. Ensure it is configured on the service provider"); + if (enabled) RecurringJob.AddOrUpdate( recurringJobId, From b26b9050d0953a38478e8ca176f9f3998d421adc Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 9 May 2020 16:51:12 +0200 Subject: [PATCH 32/37] Update BackgroundJobServerServiceCollectionExtensions.cs --- .../BackgroundJobServerServiceCollectionExtensions.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs index f4dc20b..262b09d 100644 --- a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs +++ b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs @@ -22,13 +22,11 @@ public static IServiceCollection AddHangfireBackgroundJobServer(this IServiceCol .Configure(config) ; - JobStorage.Current = new PostgreSqlStorage(settings.ConnectionString); if (!string.IsNullOrEmpty(settings.ConnectionString)) { services .AddHangfire(o => o - .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() @@ -38,7 +36,7 @@ public static IServiceCollection AddHangfireBackgroundJobServer(this IServiceCol services .AddTransient(); - + return services; } From 9abd49a436f4d996c8eb030627dda8f392af1ae3 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 9 May 2020 16:51:25 +0200 Subject: [PATCH 33/37] Update DatabaseServiceCollectionExtensions.cs --- .../Configuration/DatabaseServiceCollectionExtensions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Microservice.Db/Configuration/DatabaseServiceCollectionExtensions.cs b/Microservice.Db/Configuration/DatabaseServiceCollectionExtensions.cs index 18fa5f6..acbff89 100644 --- a/Microservice.Db/Configuration/DatabaseServiceCollectionExtensions.cs +++ b/Microservice.Db/Configuration/DatabaseServiceCollectionExtensions.cs @@ -7,7 +7,8 @@ public static class DatabaseServiceCollectionExtensions { public static IServiceCollection AddDatabase(this IServiceCollection services, string dbConnectionString) { - services.AddDbContext(o => o.UseNpgsql(dbConnectionString)); + services + .AddDbContext(o => o.UseNpgsql(dbConnectionString)); return services; } } From ca46b6a7b7171f219513e87f338e24e0b8b048e6 Mon Sep 17 00:00:00 2001 From: "armandjordaan@live.co.za" Date: Sat, 9 May 2020 16:52:15 +0200 Subject: [PATCH 34/37] Update Microservice.Api.csproj --- Microservice.Api/Microservice.Api.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Microservice.Api/Microservice.Api.csproj b/Microservice.Api/Microservice.Api.csproj index 7a1146e..2c52049 100644 --- a/Microservice.Api/Microservice.Api.csproj +++ b/Microservice.Api/Microservice.Api.csproj @@ -27,8 +27,4 @@ - - - - From 0d6e90aa8c6704dba74bacf69cb977a1d34f0c1c Mon Sep 17 00:00:00 2001 From: Armand Date: Sat, 13 Jun 2020 15:23:31 +0200 Subject: [PATCH 35/37] Install RabbitMqMessageBrokerExtensions --- .../Infrastructure/BaseControllerTest.cs | 4 ++-- .../Infrastructure/BaseEventSubscriptionTest.cs | 2 +- .../Microservice.Api.Integration.Tests.csproj | 3 ++- Microservice.Api/Microservice.Api.csproj | 6 +++--- Microservice.Api/Startup.cs | 2 +- .../EventPublishers/OrderCreatedEventPublisher.cs | 2 +- .../EventPublishers/OrderPatchedEventPublisher.cs | 2 +- .../EventPublishers/OrderPlacedEventPublisher.cs | 2 +- ...ce.RabbitMessageBroker.Integration.Tests.csproj | 14 +++++++------- .../RabbitMessageBusTests.cs | 3 ++- .../MessageBrokerSubscriptionBackgroundJob.cs | 2 +- .../Microservice.RabbitMessageBrokerHelpers.csproj | 5 ++++- Microservice.sln | 6 ------ 13 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs b/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs index 08137f6..c039cc1 100644 --- a/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs +++ b/Microservice.Api.Integration.Tests/Infrastructure/BaseControllerTest.cs @@ -1,6 +1,6 @@ using Microservice.Db; -using Microservice.RabbitMessageBroker; -using Microservice.RabbitMessageBroker.Configuration; +using Microservice.RabbitMQMessageBrokerExtension; +using Microservice.RabbitMQMessageBrokerExtension.Configuration; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; diff --git a/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs b/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs index 3d85f6d..4f98e9e 100644 --- a/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs +++ b/Microservice.Api.Integration.Tests/Infrastructure/BaseEventSubscriptionTest.cs @@ -1,4 +1,4 @@ -using Microservice.RabbitMessageBroker; +using Microservice.RabbitMQMessageBrokerExtension; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.Configuration; diff --git a/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj b/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj index 65af76a..33fabf0 100644 --- a/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj +++ b/Microservice.Api.Integration.Tests/Microservice.Api.Integration.Tests.csproj @@ -21,6 +21,7 @@ + @@ -39,7 +40,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Microservice.Api/Microservice.Api.csproj b/Microservice.Api/Microservice.Api.csproj index 2c52049..4012670 100644 --- a/Microservice.Api/Microservice.Api.csproj +++ b/Microservice.Api/Microservice.Api.csproj @@ -10,6 +10,7 @@ + @@ -22,9 +23,8 @@ - + - - + diff --git a/Microservice.Api/Startup.cs b/Microservice.Api/Startup.cs index 21af1bc..5e35b4c 100644 --- a/Microservice.Api/Startup.cs +++ b/Microservice.Api/Startup.cs @@ -7,7 +7,7 @@ using Microservice.Logic.BackgroundProcessing; using Microservice.Logic.Configuration; using Microservice.Logic.Orders.Validators; -using Microservice.RabbitMessageBroker.Configuration; +using Microservice.RabbitMQMessageBrokerExtension.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; diff --git a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs index 8d32e58..14f8809 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderCreatedEventPublisher.cs @@ -1,6 +1,6 @@ using Microservice.Logic.Orders.Events; -using Microservice.RabbitMessageBroker; using Microservice.RabbitMessageBrokerHelpers.Handlers; +using Microservice.RabbitMQMessageBrokerExtension; using System.Threading.Tasks; namespace Microservice.Logic.Orders.EventPublishers diff --git a/Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs index 4550ae6..e0e2551 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderPatchedEventPublisher.cs @@ -1,6 +1,6 @@ using Microservice.Logic.Orders.Events; -using Microservice.RabbitMessageBroker; using Microservice.RabbitMessageBrokerHelpers.Handlers; +using Microservice.RabbitMQMessageBrokerExtension; using System.Threading.Tasks; namespace Microservice.Logic.Orders.EventPublishers diff --git a/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs b/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs index fe6b87a..ba00748 100644 --- a/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs +++ b/Microservice.Logic/Orders/EventPublishers/OrderPlacedEventPublisher.cs @@ -1,6 +1,6 @@ using Microservice.Logic.Orders.Events; -using Microservice.RabbitMessageBroker; using Microservice.RabbitMessageBrokerHelpers.Handlers; +using Microservice.RabbitMQMessageBrokerExtension; using System.Threading.Tasks; namespace Microservice.Logic.Orders.EventPublishers diff --git a/Microservice.RabbitMessageBroker.Integration.Tests/Microservice.RabbitMessageBroker.Integration.Tests.csproj b/Microservice.RabbitMessageBroker.Integration.Tests/Microservice.RabbitMessageBroker.Integration.Tests.csproj index 4f12b18..3b0101c 100644 --- a/Microservice.RabbitMessageBroker.Integration.Tests/Microservice.RabbitMessageBroker.Integration.Tests.csproj +++ b/Microservice.RabbitMessageBroker.Integration.Tests/Microservice.RabbitMessageBroker.Integration.Tests.csproj @@ -20,14 +20,14 @@ - + + - - - - - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs b/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs index 18504a8..bf62e9f 100644 --- a/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs +++ b/Microservice.RabbitMessageBroker.Integration.Tests/RabbitMessageBusTests.cs @@ -1,4 +1,5 @@ -using Microservice.RabbitMessageBroker.Configuration; +using Microservice.RabbitMQMessageBrokerExtension; +using Microservice.RabbitMQMessageBrokerExtension.Configuration; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; diff --git a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs index 400154f..968db6f 100644 --- a/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs +++ b/Microservice.RabbitMessageBrokerHelpers/BackgroundJobs/MessageBrokerSubscriptionBackgroundJob.cs @@ -1,8 +1,8 @@ using Microservice.HangfireBackgroundJobServer.Infrastructure; -using Microservice.RabbitMessageBroker; using Microservice.RabbitMessageBrokerHelpers.Builders; using Microservice.RabbitMessageBrokerHelpers.Handlers; using Microservice.RabbitMessageBrokerHelpers.Models; +using Microservice.RabbitMQMessageBrokerExtension; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; diff --git a/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj b/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj index eadb8f3..523ef31 100644 --- a/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj +++ b/Microservice.RabbitMessageBrokerHelpers/Microservice.RabbitMessageBrokerHelpers.csproj @@ -8,9 +8,12 @@ + + + + - diff --git a/Microservice.sln b/Microservice.sln index 923b1df..bbcffaa 100644 --- a/Microservice.sln +++ b/Microservice.sln @@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.Db", "Microser EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.RabbitMessageBroker.Integration.Tests", "Microservice.RabbitMessageBroker.Integration.Tests\Microservice.RabbitMessageBroker.Integration.Tests.csproj", "{40BDA940-4DE3-45BD-B6B8-9761B9F5DA77}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.RabbitMessageBroker", "Microservice.RabbitMessageBroker\Microservice.RabbitMessageBroker.csproj", "{C58B170B-B159-4549-8DE1-5DC229535139}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.HangfireBackgroundJobServer", "Microservice.HangfireBackgroundJobServer\Microservice.HangfireBackgroundJobServer.csproj", "{96C85DBD-DE58-4D02-A01B-E10D521CB089}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservice.RabbitMessageBrokerHelpers", "Microservice.RabbitMessageBrokerHelpers\Microservice.RabbitMessageBrokerHelpers.csproj", "{CB07E0EA-3F3A-43BE-A77D-4BC77DD24A80}" @@ -45,10 +43,6 @@ Global {40BDA940-4DE3-45BD-B6B8-9761B9F5DA77}.Debug|Any CPU.Build.0 = Debug|Any CPU {40BDA940-4DE3-45BD-B6B8-9761B9F5DA77}.Release|Any CPU.ActiveCfg = Release|Any CPU {40BDA940-4DE3-45BD-B6B8-9761B9F5DA77}.Release|Any CPU.Build.0 = Release|Any CPU - {C58B170B-B159-4549-8DE1-5DC229535139}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C58B170B-B159-4549-8DE1-5DC229535139}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C58B170B-B159-4549-8DE1-5DC229535139}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C58B170B-B159-4549-8DE1-5DC229535139}.Release|Any CPU.Build.0 = Release|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Debug|Any CPU.Build.0 = Debug|Any CPU {96C85DBD-DE58-4D02-A01B-E10D521CB089}.Release|Any CPU.ActiveCfg = Release|Any CPU From 9484b392aded4fb0af63dd9202c78ec2c37f3941 Mon Sep 17 00:00:00 2001 From: Armand Date: Sat, 13 Jun 2020 15:41:03 +0200 Subject: [PATCH 36/37] Update dockerfile --- Dockerfile | 2 +- .../MessageBrokerPublishersConfigurationbuilder.cs | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) delete mode 100644 Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs diff --git a/Dockerfile b/Dockerfile index 0458613..7e7ac18 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ COPY ./Microservice.Api/*.csproj ./Microservice.Api/ COPY ./Microservice.Db/*.csproj ./Microservice.Db/ COPY ./Microservice.Logic/*.csproj ./Microservice.Logic/ COPY ./Microservice.HangfireBackgroundJobServer/*.csproj ./Microservice.HangfireBackgroundJobServer/ -COPY ./Microservice.RabbitMessageBroker/*.csproj ./Microservice.RabbitMessageBroker/ +COPY ./Microservice.RabbitMessageBrokerHelpers/*.csproj ./Microservice.RabbitMessageBrokerHelpers/ COPY ./Microservice.RabbitMessageBroker.Integration.Tests/*.csproj ./Microservice.RabbitMessageBroker.Integration.Tests/ COPY ./Microservice.Api.Integration.Tests/*.csproj ./Microservice.Api.Integration.Tests/ RUN dotnet restore diff --git a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs b/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs deleted file mode 100644 index f010049..0000000 --- a/Microservice.RabbitMessageBrokerHelpers/Builders/MessageBrokerPublishersConfigurationbuilder.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Microservice.RabbitMessageBrokerHelpers.Builders -{ - public class MessageBrokerPublishersConfigurationBuilder - { - - } -} From 0661690007e5c430864ddaf63c90a635efe72ca2 Mon Sep 17 00:00:00 2001 From: Armand Date: Sat, 4 Jul 2020 21:01:21 +0200 Subject: [PATCH 37/37] Cleanup --- .../BackgroundJobServerServiceCollectionExtensions.cs | 1 - .../OrderPlacedEventSubscriptionJob.cs | 10 ---------- 2 files changed, 11 deletions(-) delete mode 100644 Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs diff --git a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs index 262b09d..9b71fc5 100644 --- a/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs +++ b/Microservice.HangfireBackgroundJobServer/Configuration/BackgroundJobServerServiceCollectionExtensions.cs @@ -22,7 +22,6 @@ public static IServiceCollection AddHangfireBackgroundJobServer(this IServiceCol .Configure(config) ; - if (!string.IsNullOrEmpty(settings.ConnectionString)) { services diff --git a/Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs b/Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs deleted file mode 100644 index c0f7c22..0000000 --- a/Microservice.Logic/BackgroundProcessing/OrderPlacedEventSubscriptionJob.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Microservice.Logic.BackgroundProcessing -{ - public class OrderPlacedEventSubscriptionJob - { - public OrderPlacedEventSubscriptionJob() - { - - } - } -}