Browse Source

Refactor Ordering SignalrHub eventbus using CAP

pull/970/head
Savorboard 6 years ago
parent
commit
5914c9d606
18 changed files with 126 additions and 301 deletions
  1. +2
    -4
      src/Services/Ordering/Ordering.BackgroundTasks/IntegrationEvents/GracePeriodConfirmedIntegrationEvent.cs
  2. +6
    -9
      src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj
  3. +28
    -91
      src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs
  4. +5
    -5
      src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs
  5. +6
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs
  6. +4
    -6
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs
  7. +3
    -3
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs
  8. +3
    -5
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs
  9. +5
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs
  10. +5
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs
  11. +2
    -5
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs
  12. +2
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs
  13. +2
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs
  14. +2
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs
  15. +2
    -4
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs
  16. +2
    -8
      src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToSubmittedIntegrationEvent.cs
  17. +6
    -12
      src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj
  18. +41
    -101
      src/Services/Ordering/Ordering.SignalrHub/Startup.cs

+ 2
- 4
src/Services/Ordering/Ordering.BackgroundTasks/IntegrationEvents/GracePeriodConfirmedIntegrationEvent.cs View File

@ -1,8 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Ordering.BackgroundTasks.IntegrationEvents
namespace Ordering.BackgroundTasks.IntegrationEvents
{ {
public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent
public class GracePeriodConfirmedIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }


+ 6
- 9
src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj View File

@ -18,7 +18,11 @@
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.0" /> <PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.0" />
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.0" /> <PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" /> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" />
<PackageReference Include="Dapper" Version="1.50.7" />
<PackageReference Include="Dapper" Version="1.60.1" />
<PackageReference Include="DotNetCore.CAP" Version="2.5.0-preview-69210974" />
<PackageReference Include="DotNetCore.CAP.AzureServiceBus" Version="2.5.0-preview-69210974" />
<PackageReference Include="DotNetCore.CAP.RabbitMQ" Version="2.5.0-preview-69210974" />
<PackageReference Include="DotNetCore.CAP.InMemoryStorage" Version="2.5.0-preview-69210974" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" /> <PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
@ -28,12 +32,5 @@
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" /> <PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" /> <PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
</ItemGroup>
</ItemGroup>
</Project> </Project>

+ 28
- 91
src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs View File

@ -1,19 +1,11 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.ServiceBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Ordering.BackgroundTasks.Configuration; using Ordering.BackgroundTasks.Configuration;
using Ordering.BackgroundTasks.Tasks; using Ordering.BackgroundTasks.Tasks;
using RabbitMQ.Client;
using System; using System;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks;
@ -46,58 +38,43 @@ namespace Ordering.BackgroundTasks
services.AddSingleton<IHostedService, GracePeriodManagerService>(); services.AddSingleton<IHostedService, GracePeriodManagerService>();
//configure event bus related services //configure event bus related services
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
services.AddCap(options =>
{ {
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
options.UseInMemoryStorage();
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{ {
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
var serviceBusConnectionString = Configuration["EventBusConnection"];
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
});
}
else
{
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
options.UseAzureServiceBus(Configuration["EventBusConnection"]);
}
else
{ {
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"]
};
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
options.UseRabbitMQ(conf =>
{ {
factory.UserName = Configuration["EventBusUserName"];
}
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
{
factory.Password = Configuration["EventBusPassword"];
}
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
});
}
conf.HostName = Configuration["EventBusConnection"];
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
{
conf.UserName = Configuration["EventBusUserName"];
}
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
{
conf.Password = Configuration["EventBusPassword"];
}
});
}
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
RegisterEventBus(services);
if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"]))
{
options.DefaultGroup = Configuration["SubscriptionClientName"];
}
});
//create autofac based service provider //create autofac based service provider
var container = new ContainerBuilder(); var container = new ContainerBuilder();
container.Populate(services); container.Populate(services);
return new AutofacServiceProvider(container.Build()); return new AutofacServiceProvider(container.Build());
} }
@ -116,46 +93,6 @@ namespace Ordering.BackgroundTasks
Predicate = r => r.Name.Contains("self") Predicate = r => r.Name.Contains("self")
}); });
} }
private void RegisterEventBus(IServiceCollection services)
{
var subscriptionClientName = Configuration["SubscriptionClientName"];
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
{
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
});
}
else
{
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
{
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
});
}
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
}
} }
public static class CustomExtensionMethods public static class CustomExtensionMethods


+ 5
- 5
src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs View File

@ -1,5 +1,4 @@
using Dapper; using Dapper;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -10,6 +9,7 @@ using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.BackgroundTasks.Tasks namespace Ordering.BackgroundTasks.Tasks
{ {
@ -18,11 +18,11 @@ namespace Ordering.BackgroundTasks.Tasks
{ {
private readonly ILogger<GracePeriodManagerService> _logger; private readonly ILogger<GracePeriodManagerService> _logger;
private readonly BackgroundTaskSettings _settings; private readonly BackgroundTaskSettings _settings;
private readonly IEventBus _eventBus;
private readonly ICapPublisher _eventBus;
public GracePeriodManagerService( public GracePeriodManagerService(
IOptions<BackgroundTaskSettings> settings, IOptions<BackgroundTaskSettings> settings,
IEventBus eventBus,
ICapPublisher eventBus,
ILogger<GracePeriodManagerService> logger) ILogger<GracePeriodManagerService> logger)
{ {
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
@ -61,9 +61,9 @@ namespace Ordering.BackgroundTasks.Tasks
{ {
var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent(orderId); var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent(orderId);
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", confirmGracePeriodEvent.Id, Program.AppName, confirmGracePeriodEvent);
_logger.LogInformation("----- Publishing integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, confirmGracePeriodEvent);
_eventBus.Publish(confirmGracePeriodEvent);
_eventBus.Publish(nameof(GracePeriodConfirmedIntegrationEvent), confirmGracePeriodEvent);
} }
} }


+ 6
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs View File

@ -1,15 +1,13 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Serilog.Context; using Serilog.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.SignalrHub.IntegrationEvents namespace Ordering.SignalrHub.IntegrationEvents
{ {
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : ICapSubscribe
{ {
private readonly IHubContext<NotificationsHub> _hubContext; private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<OrderStatusChangedToAwaitingValidationIntegrationEventHandler> _logger; private readonly ILogger<OrderStatusChangedToAwaitingValidationIntegrationEventHandler> _logger;
@ -22,12 +20,12 @@ namespace Ordering.SignalrHub.IntegrationEvents
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
//TODO: [CapSubscribe(nameof(OrderStatusChangedToAwaitingValidationIntegrationEvent))]
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event) public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event)
{ {
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}"))
{ {
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_logger.LogInformation("----- Handling integration event:{AppName} - ({@IntegrationEvent})", Program.AppName, @event);
await _hubContext.Clients await _hubContext.Clients
.Group(@event.BuyerName) .Group(@event.BuyerName)


+ 4
- 6
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToCancelledIntegrationEventHandler.cs View File

@ -1,16 +1,14 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.IntegrationEvents.Events; using Ordering.SignalrHub.IntegrationEvents.Events;
using Serilog.Context; using Serilog.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
{ {
public class OrderStatusChangedToCancelledIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToCancelledIntegrationEvent>
public class OrderStatusChangedToCancelledIntegrationEventHandler : ICapSubscribe
{ {
private readonly IHubContext<NotificationsHub> _hubContext; private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<OrderStatusChangedToCancelledIntegrationEventHandler> _logger; private readonly ILogger<OrderStatusChangedToCancelledIntegrationEventHandler> _logger;
@ -23,7 +21,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
//TODO: [CapSubscribe(nameof(OrderStatusChangedToCancelledIntegrationEvent))]
public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event) public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event)
{ {
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))


+ 3
- 3
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs View File

@ -1,14 +1,14 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.IntegrationEvents.Events; using Ordering.SignalrHub.IntegrationEvents.Events;
using Serilog.Context; using Serilog.Context;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
{ {
public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
public class OrderStatusChangedToPaidIntegrationEventHandler : ICapSubscribe
{ {
private readonly IHubContext<NotificationsHub> _hubContext; private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<OrderStatusChangedToPaidIntegrationEventHandler> _logger; private readonly ILogger<OrderStatusChangedToPaidIntegrationEventHandler> _logger;
@ -21,7 +21,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
//TODO [CapSubscribe(nameof(OrderStatusChangedToPaidIntegrationEvent))]
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event)
{ {
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))


+ 3
- 5
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToShippedIntegrationEventHandler.cs View File

@ -1,16 +1,14 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.IntegrationEvents.Events; using Ordering.SignalrHub.IntegrationEvents.Events;
using Serilog.Context; using Serilog.Context;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
{ {
public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent>
public class OrderStatusChangedToShippedIntegrationEventHandler : ICapSubscribe
{ {
private readonly IHubContext<NotificationsHub> _hubContext; private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<OrderStatusChangedToShippedIntegrationEventHandler> _logger; private readonly ILogger<OrderStatusChangedToShippedIntegrationEventHandler> _logger;
@ -23,7 +21,7 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
//TODO [CapSubscribe(nameof(OrderStatusChangedToShippedIntegrationEvent))]
public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event)
{ {
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))


+ 5
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs View File

@ -1,17 +1,14 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.IntegrationEvents.Events; using Ordering.SignalrHub.IntegrationEvents.Events;
using Serilog.Context; using Serilog.Context;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
{ {
public class OrderStatusChangedToStockConfirmedIntegrationEventHandler :
IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>
public class OrderStatusChangedToStockConfirmedIntegrationEventHandler :ICapSubscribe
{ {
private readonly IHubContext<NotificationsHub> _hubContext; private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<OrderStatusChangedToStockConfirmedIntegrationEventHandler> _logger; private readonly ILogger<OrderStatusChangedToStockConfirmedIntegrationEventHandler> _logger;
@ -24,12 +21,12 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
//TODO [CapSubscribe(nameof(OrderStatusChangedToStockConfirmedIntegrationEvent))]
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event)
{ {
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}"))
{ {
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event);
await _hubContext.Clients await _hubContext.Clients
.Group(@event.BuyerName) .Group(@event.BuyerName)


+ 5
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs View File

@ -1,17 +1,14 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.IntegrationEvents.Events; using Ordering.SignalrHub.IntegrationEvents.Events;
using Serilog.Context; using Serilog.Context;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP;
namespace Ordering.SignalrHub.IntegrationEvents.EventHandling namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
{ {
public class OrderStatusChangedToSubmittedIntegrationEventHandler :
IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent>
public class OrderStatusChangedToSubmittedIntegrationEventHandler :ICapSubscribe
{ {
private readonly IHubContext<NotificationsHub> _hubContext; private readonly IHubContext<NotificationsHub> _hubContext;
private readonly ILogger<OrderStatusChangedToSubmittedIntegrationEventHandler> _logger; private readonly ILogger<OrderStatusChangedToSubmittedIntegrationEventHandler> _logger;
@ -24,12 +21,12 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
} }
//TODO [CapSubscribe(nameof(OrderStatusChangedToSubmittedIntegrationEvent))]
public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event) public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event)
{ {
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}"))
{ {
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
_logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event);
await _hubContext.Clients await _hubContext.Clients
.Group(@event.BuyerName) .Group(@event.BuyerName)


+ 2
- 5
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs View File

@ -1,9 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System.Collections.Generic;
namespace Ordering.SignalrHub.IntegrationEvents
namespace Ordering.SignalrHub.IntegrationEvents
{ {
public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent
public class OrderStatusChangedToAwaitingValidationIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }
public string OrderStatus { get; } public string OrderStatus { get; }


+ 2
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToCancelledIntegrationEvent.cs View File

@ -1,12 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.SignalrHub.IntegrationEvents.Events
namespace Ordering.SignalrHub.IntegrationEvents.Events
{ {
public class OrderStatusChangedToCancelledIntegrationEvent : IntegrationEvent
public class OrderStatusChangedToCancelledIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }
public string OrderStatus { get; } public string OrderStatus { get; }


+ 2
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs View File

@ -1,12 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.SignalrHub.IntegrationEvents.Events
namespace Ordering.SignalrHub.IntegrationEvents.Events
{ {
public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent
public class OrderStatusChangedToPaidIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }
public string OrderStatus { get; } public string OrderStatus { get; }


+ 2
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToShippedIntegrationEvent.cs View File

@ -1,12 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.SignalrHub.IntegrationEvents.Events
namespace Ordering.SignalrHub.IntegrationEvents.Events
{ {
public class OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent
public class OrderStatusChangedToShippedIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }
public string OrderStatus { get; } public string OrderStatus { get; }


+ 2
- 4
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs View File

@ -1,8 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Ordering.SignalrHub.IntegrationEvents.Events
namespace Ordering.SignalrHub.IntegrationEvents.Events
{ {
public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent
public class OrderStatusChangedToStockConfirmedIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }
public string OrderStatus { get; } public string OrderStatus { get; }


+ 2
- 8
src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToSubmittedIntegrationEvent.cs View File

@ -1,12 +1,6 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.SignalrHub.IntegrationEvents.Events
namespace Ordering.SignalrHub.IntegrationEvents.Events
{ {
public class OrderStatusChangedToSubmittedIntegrationEvent : IntegrationEvent
public class OrderStatusChangedToSubmittedIntegrationEvent
{ {
public int OrderId { get; } public int OrderId { get; }
public string OrderStatus { get; } public string OrderStatus { get; }


+ 6
- 12
src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj View File

@ -4,16 +4,16 @@
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath> <DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.0" /> <PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="2.2.0" />
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.0" /> <PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.2" /> <PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.2" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" /> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" />
<PackageReference Include="DotNetCore.CAP" Version="2.5.0-preview-69210974" />
<PackageReference Include="DotNetCore.CAP.AzureServiceBus" Version="2.5.0-preview-69210974" />
<PackageReference Include="DotNetCore.CAP.RabbitMQ" Version="2.5.0-preview-69210974" />
<PackageReference Include="DotNetCore.CAP.InMemoryStorage" Version="2.5.0-preview-69210974" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.1" /> <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.1" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.1" /> <PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.1" />
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.2" /> <PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.2" />
@ -30,12 +30,6 @@
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" /> <PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" /> <PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
</ItemGroup>
</ItemGroup>
</Project> </Project>

+ 41
- 101
src/Services/Ordering/Ordering.SignalrHub/Startup.cs View File

@ -2,19 +2,12 @@
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Azure.ServiceBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.SignalrHub.AutofacModules; using Ordering.SignalrHub.AutofacModules;
using Ordering.SignalrHub.IntegrationEvents; using Ordering.SignalrHub.IntegrationEvents;
using Ordering.SignalrHub.IntegrationEvents.EventHandling; using Ordering.SignalrHub.IntegrationEvents.EventHandling;
using Ordering.SignalrHub.IntegrationEvents.Events;
using RabbitMQ.Client;
using System; using System;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
@ -59,54 +52,43 @@ namespace Ordering.SignalrHub
services.AddSignalR(); services.AddSignalR();
} }
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
services.AddIntegrationEventHandler();
services.AddCap(options =>
{ {
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
options.UseInMemoryStorage();
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{ {
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
var serviceBusConnectionString = Configuration["EventBusConnection"];
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
});
}
else
{
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
options.UseAzureServiceBus(Configuration["EventBusConnection"]);
}
else
{ {
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"]
};
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
{
factory.UserName = Configuration["EventBusUserName"];
}
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
options.UseRabbitMQ(conf =>
{ {
factory.Password = Configuration["EventBusPassword"];
}
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
conf.HostName = Configuration["EventBusConnection"];
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
{
conf.UserName = Configuration["EventBusUserName"];
}
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
{
conf.Password = Configuration["EventBusPassword"];
}
});
}
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
});
}
if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"]))
{
options.DefaultGroup = Configuration["SubscriptionClientName"];
}
});
ConfigureAuthService(services); ConfigureAuthService(services);
RegisterEventBus(services);
services.AddOptions(); services.AddOptions();
//configure autofac //configure autofac
@ -151,21 +133,7 @@ namespace Ordering.SignalrHub
{ {
routes.MapHub<NotificationsHub>("/notificationhub", options => routes.MapHub<NotificationsHub>("/notificationhub", options =>
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransports.All); options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransports.All);
});
ConfigureEventBus(app);
}
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent, OrderStatusChangedToStockConfirmedIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToCancelledIntegrationEvent, OrderStatusChangedToCancelledIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToSubmittedIntegrationEvent, OrderStatusChangedToSubmittedIntegrationEventHandler>();
});
} }
private void ConfigureAuthService(IServiceCollection services) private void ConfigureAuthService(IServiceCollection services)
@ -187,49 +155,21 @@ namespace Ordering.SignalrHub
options.Audience = "orders.signalrhub"; options.Audience = "orders.signalrhub";
}); });
} }
private void RegisterEventBus(IServiceCollection services)
{
var subscriptionClientName = Configuration["SubscriptionClientName"];
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
{
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
});
}
else
{
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
{
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
});
}
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
}
} }
public static class CustomExtensionMethods public static class CustomExtensionMethods
{ {
public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services)
{
services.AddTransient<OrderStatusChangedToAwaitingValidationIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToAwaitingValidationIntegrationEvent
services.AddTransient<OrderStatusChangedToPaidIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToPaidIntegrationEvent
services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToStockConfirmedIntegrationEvent
services.AddTransient<OrderStatusChangedToShippedIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToShippedIntegrationEvent
services.AddTransient<OrderStatusChangedToCancelledIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToCancelledIntegrationEvent
services.AddTransient<OrderStatusChangedToSubmittedIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToSubmittedIntegrationEvent
return services;
}
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
{ {
var hcBuilder = services.AddHealthChecks(); var hcBuilder = services.AddHealthChecks();


Loading…
Cancel
Save