@ -2,19 +2,12 @@
using Autofac.Extensions.DependencyInjection ;
using Microsoft.AspNetCore.Authentication.JwtBearer ;
using Microsoft.AspNetCore.Builder ;
using Microsoft.Azure.ServiceBus ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus ;
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Logging ;
using Ordering.SignalrHub.AutofacModules ;
using Ordering.SignalrHub.IntegrationEvents ;
using Ordering.SignalrHub.IntegrationEvents.EventHandling ;
using Ordering.SignalrHub.IntegrationEvents.Events ;
using RabbitMQ.Client ;
using System ;
using System.IdentityModel.Tokens.Jwt ;
using HealthChecks.UI.Client ;
@ -59,54 +52,43 @@ namespace Ordering.SignalrHub
services . AddSignalR ( ) ;
}
if ( Configuration . GetValue < 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 ) ;
RegisterEventBus ( services ) ;
services . AddOptions ( ) ;
//configure autofac
@ -151,21 +133,7 @@ namespace Ordering.SignalrHub
{
routes . MapHub < NotificationsHub > ( "/notificationhub" , options = >
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 )
@ -187,49 +155,21 @@ namespace Ordering.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 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 )
{
var hcBuilder = services . AddHealthChecks ( ) ;