From 748a0596d314619fe279089ffa667a639863f7d1 Mon Sep 17 00:00:00 2001 From: espent1004 Date: Wed, 5 Feb 2020 22:15:48 +0100 Subject: [PATCH] Implementing IMultiEventBus in all baseline microservices. Still need to ensure that tenantId is set for all events. --- .../EventBusRabbitMQ/MultiEventBusRabbitMQ.cs | 18 +- src/Services/Basket/Basket.API/Startup.cs | 204 +++++------------- .../CatalogIntegrationEventService.cs | 4 +- src/Services/Catalog/Catalog.API/Startup.cs | 105 +++++---- .../Services/LocationsService.cs | 4 +- .../Location/Locations.API/Startup.cs | 189 ++++++++-------- .../Marketing/Marketing.API/Startup.cs | 117 +++++----- ...CheckoutAcceptedIntegrationEventHandler.cs | 4 +- .../OrderingIntegrationEventService.cs | 4 +- src/Services/Ordering/Ordering.API/Startup.cs | 103 ++++----- .../Ordering.BackgroundTasks/Startup.cs | 140 ++++++------ .../Tasks/GracePeriodManagerTask.cs | 4 +- .../Ordering/Ordering.SignalrHub/Startup.cs | 180 ++++++++-------- ...ToStockConfirmedIntegrationEventHandler.cs | 4 +- src/Services/Payment/Payment.API/Startup.cs | 140 ++++++------ src/Services/Webhooks/Webhooks.API/Startup.cs | 113 +++++----- 16 files changed, 602 insertions(+), 731 deletions(-) diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/MultiEventBusRabbitMQ.cs b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/MultiEventBusRabbitMQ.cs index 9a92a1eb7..f386f9b41 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/MultiEventBusRabbitMQ.cs +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/MultiEventBusRabbitMQ.cs @@ -8,10 +8,12 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ public class MultiEventBusRabbitMQ : IMultiEventBus { private List _eventBuses; + private Dictionary _tenants; - public MultiEventBusRabbitMQ(List eventBuses) + public MultiEventBusRabbitMQ(List eventBuses, Dictionary tenants) { _eventBuses = eventBuses; + _tenants = tenants; } public void AddEventBus(IEventBus eventBus) @@ -21,13 +23,17 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ public void Publish(IntegrationEvent @event) { - //TODO - var actualEventBus = _eventBuses.Find(e => e.GetVHost().Equals("TenantA")); - - if (actualEventBus == null) + if (@event.TenantId == 0)//System wide event? { - throw new Exception(); + _eventBuses.ForEach(eventBus => + { + eventBus.Publish(@event); + }); } + + //TODO requires ALL events to have tenantId set! + _tenants.TryGetValue(@event.TenantId, out String tenantName); + var actualEventBus = _eventBuses.Find(e => e.GetVHost().Equals(tenantName)); actualEventBus.Publish(@event); } diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index 8892b812a..11413e929 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -5,7 +5,6 @@ using Basket.API.Infrastructure.Middlewares; using Basket.API.IntegrationEvents.EventHandling; using Basket.API.IntegrationEvents.Events; using HealthChecks.UI.Client; - using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.ServiceFabric; using Microsoft.AspNetCore.Authentication.JwtBearer; @@ -58,7 +57,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); options.Filters.Add(typeof(ValidateModelStateFilter)); - }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddControllersAsServices(); @@ -85,69 +83,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API return ConnectionMultiplexer.Connect(configuration); }); - - -/* if (Configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - }*/ -/* else + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } - - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - - - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - - });*/ - - services.AddSingleton(sp => - { - IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); - connections.AddConnection(GenerateConnection("TenantA", sp)); - connections.AddConnection(GenerateConnection("TenantB", sp)); - connections.AddConnection(GenerateConnection("/", sp)); - - return connections; - }); + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp)); + connections.AddConnection(GenerateConnection("TenantB", sp)); + return connections; + }); - //} RegisterEventBus(services); @@ -170,7 +114,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API TokenUrl = $"{Configuration.GetValue("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary() { - { "basket", "Basket API" } + {"basket", "Basket API"} } }); @@ -181,10 +125,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API { options.AddPolicy("CorsPolicy", builder => builder - .SetIsOriginAllowed((host) => true) - .AllowAnyMethod() - .AllowAnyHeader() - .AllowCredentials()); + .SetIsOriginAllowed((host) => true) + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials()); }); services.AddSingleton(); services.AddTransient(); @@ -226,9 +170,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API { retryCount = int.Parse(Configuration["EventBusRetryCount"]); } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -254,7 +197,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API Predicate = r => r.Name.Contains("self") }); - app.UseStaticFiles(); + app.UseStaticFiles(); app.UseCors("CorsPolicy"); ConfigureAuth(app); @@ -262,27 +205,29 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API app.UseMvcWithDefaultRoute(); app.UseSwagger() - .UseSwaggerUI(c => - { - c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Basket.API V1"); - c.OAuthClientId ("basketswaggerui"); - c.OAuthAppName("Basket Swagger UI"); - }); + .UseSwaggerUI(c => + { + c.SwaggerEndpoint( + $"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", + "Basket.API V1"); + c.OAuthClientId("basketswaggerui"); + c.OAuthAppName("Basket Swagger UI"); + }); ConfigureEventBus(app); - } private void RegisterAppInsights(IServiceCollection services) { services.AddApplicationInsightsTelemetry(Configuration); var orchestratorType = Configuration.GetValue("OrchestratorType"); - + if (orchestratorType?.ToUpper() == "K8S") { // Enable K8s telemetry initializer services.AddApplicationInsightsKubernetesEnricher(); } + if (orchestratorType?.ToUpper() == "SF") { // Enable SF telemetry initializer @@ -296,13 +241,12 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API // prevent from mapping "sub" claim to nameidentifier. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); - var identityUrl = Configuration.GetValue("IdentityUrl"); - + var identityUrl = Configuration.GetValue("IdentityUrl"); + services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddJwtBearer(options => { options.Authority = identityUrl; @@ -325,76 +269,31 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API { var subscriptionClientName = Configuration["SubscriptionClientName"]; - /*if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else - {*/ - - services.AddSingleton(sp => - { - var multiRabbitMqPersistentConnections = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); + var iLifetimeScope = sp.GetRequiredService(); + var logger = sp.GetRequiredService>(); + var eventBusSubcriptionsManager = sp.GetRequiredService(); - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - List eventBuses = new List(); - - eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); - eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); - - - /*multiRabbitMqPersistentConnections.GetConnections().ForEach(conn => - { - eventBuses.Add(new EventBusRabbitMQ(conn, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount)); - });*/ - - return new MultiEventBusRabbitMQ(eventBuses); - }); - - /* services.AddSingleton(sp => + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } + List eventBuses = new List(); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); - var multiRabbitMqPersistentConnections = sp.GetRequiredService(); - List testing = new List(); - - multiRabbitMqPersistentConnections.GetConnections().ForEach(conn => - { - testing.Add(new EventBusRabbitMQ(conn, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount)); - }); - - Console.WriteLine(testing); - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); - });*/ - - //} + return new MultiEventBusRabbitMQ(eventBuses, tenants); + }); services.AddSingleton(); @@ -408,22 +307,23 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API eventBus.Subscribe(); eventBus.Subscribe(); - } + } } public static class CustomExtensionMethods { - public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, + IConfiguration configuration) { var hcBuilder = services.AddHealthChecks(); hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy()); - hcBuilder + hcBuilder .AddRedis( configuration["ConnectionString"], name: "redis-check", - tags: new string[] { "redis" }); + tags: new string[] {"redis"}); if (configuration.GetValue("AzureServiceBusEnabled")) { @@ -432,7 +332,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API configuration["EventBusConnection"], topicName: "eshop_event_bus", name: "basket-servicebus-check", - tags: new string[] { "servicebus" }); + tags: new string[] {"servicebus"}); } else { @@ -440,10 +340,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API .AddRabbitMQ( $"amqp://{configuration["EventBusConnection"]}", name: "basket-rabbitmqbus-check", - tags: new string[] { "rabbitmqbus" }); + tags: new string[] {"rabbitmqbus"}); } return services; } } -} +} \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs index bb3a23d40..ce4d12728 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs @@ -17,14 +17,14 @@ namespace Catalog.API.IntegrationEvents public class CatalogIntegrationEventService : ICatalogIntegrationEventService { private readonly Func _integrationEventLogServiceFactory; - private readonly IEventBus _eventBus; + private readonly IMultiEventBus _eventBus; private readonly CatalogContext _catalogContext; private readonly IIntegrationEventLogService _eventLogService; private readonly ILogger _logger; public CatalogIntegrationEventService( ILogger logger, - IEventBus eventBus, + IMultiEventBus eventBus, CatalogContext catalogContext, Func integrationEventLogServiceFactory) { diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index c9955b7ac..ca9a24457 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -26,6 +26,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using RabbitMQ.Client; using System; +using System.Collections.Generic; using System.Data.Common; using System.Reflection; using HealthChecks.UI.Client; @@ -101,7 +102,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API protected virtual void ConfigureEventBus(IApplicationBuilder app) { - var eventBus = app.ApplicationServices.GetRequiredService(); + var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe(); eventBus.Subscribe(); } @@ -275,21 +276,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API sp => (DbConnection c) => new IntegrationEventLogService(c)); services.AddTransient(); - - if (configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var settings = sp.GetRequiredService>().Value; - var logger = sp.GetRequiredService>(); - - var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.EventBusConnection); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { + services.AddSingleton(sp => { var settings = sp.GetRequiredService>().Value; @@ -321,47 +308,79 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); }); - } + + services.AddSingleton(sp => + { + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp, configuration)); + connections.AddConnection(GenerateConnection("TenantB", sp, configuration)); + return connections; + }); return services; } - public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) + + private static IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp, IConfiguration configuration) { - var subscriptionClientName = configuration["SubscriptionClientName"]; + var logger = sp.GetRequiredService>(); -/* if (configuration.GetValue("AzureServiceBusEnabled")) + var factory = new ConnectionFactory() { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + HostName = configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); + if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) + { + factory.UserName = configuration["EventBusUserName"]; + } + if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) + { + factory.Password = configuration["EventBusPassword"]; + } + + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(configuration["EventBusRetryCount"]); } - else*/ + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } + + public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) + { + var subscriptionClientName = configuration["SubscriptionClientName"]; + + services.AddSingleton(sp => { - services.AddSingleton(sp => + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); + var iLifetimeScope = sp.GetRequiredService(); + var logger = sp.GetRequiredService>(); + var eventBusSubcriptionsManager = sp.GetRequiredService(); + + var retryCount = 5; + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + retryCount = int.Parse(configuration["EventBusRetryCount"]); + } - var retryCount = 5; - if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } + List eventBuses = new List(); - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); - }); - } + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); + }); services.AddSingleton(); services.AddTransient(); diff --git a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs index f6b9ed708..27eb53dc9 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs @@ -14,12 +14,12 @@ public class LocationsService : ILocationsService { private readonly ILocationsRepository _locationsRepository; - private readonly IEventBus _eventBus; + private readonly IMultiEventBus _eventBus; private readonly ILogger _logger; public LocationsService( ILocationsRepository locationsRepository, - IEventBus eventBus, + IMultiEventBus eventBus, ILogger logger) { _locationsRepository = locationsRepository ?? throw new ArgumentNullException(nameof(locationsRepository)); diff --git a/src/Services/Location/Locations.API/Startup.cs b/src/Services/Location/Locations.API/Startup.cs index 06f9d55af..742dd18ac 100644 --- a/src/Services/Location/Locations.API/Startup.cs +++ b/src/Services/Location/Locations.API/Startup.cs @@ -46,10 +46,7 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API services .AddCustomHealthCheck(Configuration) - .AddMvc(options => - { - options.Filters.Add(typeof(HttpGlobalExceptionFilter)); - }) + .AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddControllersAsServices(); @@ -57,51 +54,14 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API services.Configure(Configuration); - if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp)); + connections.AddConnection(GenerateConnection("TenantB", sp)); - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + return connections; + }); RegisterEventBus(services); @@ -125,22 +85,21 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API TokenUrl = $"{Configuration.GetValue("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary() { - { "locations", "Locations API" } + {"locations", "Locations API"} } }); options.OperationFilter(); - }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder - .SetIsOriginAllowed((host) => true) - .AllowAnyMethod() - .AllowAnyHeader() - .AllowCredentials()); + .SetIsOriginAllowed((host) => true) + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials()); }); services.AddSingleton(); @@ -155,6 +114,38 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API return new AutofacServiceProvider(container.Build()); } + + private IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp) + { + var logger = sp.GetRequiredService>(); + + var factory = new ConnectionFactory() + { + HostName = Configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; + + if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) + { + factory.UserName = Configuration["EventBusUserName"]; + } + + if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) + { + factory.Password = Configuration["EventBusPassword"]; + } + + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { @@ -185,12 +176,14 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API app.UseMvcWithDefaultRoute(); app.UseSwagger() - .UseSwaggerUI(c => - { - c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Locations.API V1"); - c.OAuthClientId("locationsswaggerui"); - c.OAuthAppName("Locations Swagger UI"); - }); + .UseSwaggerUI(c => + { + c.SwaggerEndpoint( + $"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", + "Locations.API V1"); + c.OAuthClientId("locationsswaggerui"); + c.OAuthAppName("Locations Swagger UI"); + }); LocationsContextSeed.SeedAsync(app, loggerFactory) .Wait(); @@ -206,6 +199,7 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API // Enable K8s telemetry initializer services.AddApplicationInsightsKubernetesEnricher(); } + if (orchestratorType?.ToUpper() == "SF") { // Enable SF telemetry initializer @@ -220,16 +214,16 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(options => - { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }) - .AddJwtBearer(options => - { - options.Authority = Configuration.GetValue("IdentityUrl"); - options.Audience = "locations"; - options.RequireHttpsMetadata = false; - }); + { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(options => + { + options.Authority = Configuration.GetValue("IdentityUrl"); + options.Audience = "locations"; + options.RequireHttpsMetadata = false; + }); } protected virtual void ConfigureAuth(IApplicationBuilder app) @@ -246,37 +240,31 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API { var subscriptionClientName = Configuration["SubscriptionClientName"]; -/* if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); + var iLifetimeScope = sp.GetRequiredService(); + var logger = sp.GetRequiredService>(); + var eventBusSubcriptionsManager = sp.GetRequiredService(); - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else*/ - { - services.AddSingleton(sp => + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } + List eventBuses = new List(); - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); - }); - } + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); + }); services.AddSingleton(); } @@ -284,7 +272,8 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API public static class CustomExtensionMethods { - public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, + IConfiguration configuration) { var hcBuilder = services.AddHealthChecks(); @@ -294,7 +283,7 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API .AddMongoDb( configuration["ConnectionString"], name: "locations-mongodb-check", - tags: new string[] { "mongodb" }); + tags: new string[] {"mongodb"}); if (configuration.GetValue("AzureServiceBusEnabled")) { @@ -303,7 +292,7 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API configuration["EventBusConnection"], topicName: "eshop_event_bus", name: "locations-servicebus-check", - tags: new string[] { "servicebus" }); + tags: new string[] {"servicebus"}); } else { @@ -311,10 +300,10 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API .AddRabbitMQ( $"amqp://{configuration["EventBusConnection"]}", name: "locations-rabbitmqbus-check", - tags: new string[] { "rabbitmqbus" }); + tags: new string[] {"rabbitmqbus"}); } return services; } } -} +} \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/Startup.cs b/src/Services/Marketing/Marketing.API/Startup.cs index 4d190be1a..533f2adc2 100644 --- a/src/Services/Marketing/Marketing.API/Startup.cs +++ b/src/Services/Marketing/Marketing.API/Startup.cs @@ -80,52 +80,17 @@ options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)); //Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval }); - - if (Configuration.GetValue("AzureServiceBusEnabled")) + + + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp)); + connections.AddConnection(GenerateConnection("TenantB", sp)); - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } - - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + return connections; + }); + // Add framework services. services.AddSwaggerGen(options => @@ -259,25 +224,10 @@ private void RegisterEventBus(IServiceCollection services) { var subscriptionClientName = Configuration["SubscriptionClientName"]; - -/* if (Configuration.GetValue("AzureServiceBusEnabled")) { - services.AddSingleton(sp => + services.AddSingleton(sp => { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else*/ - { - services.AddSingleton(sp => - { - var rabbitMQPersistentConnection = sp.GetRequiredService(); + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); var iLifetimeScope = sp.GetRequiredService(); var logger = sp.GetRequiredService>(); var eventBusSubcriptionsManager = sp.GetRequiredService(); @@ -288,17 +238,58 @@ retryCount = int.Parse(Configuration["EventBusRetryCount"]); } - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); + List eventBuses = new List(); + + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); }); } services.AddSingleton(); services.AddTransient(); } + + private IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp) + { + var logger = sp.GetRequiredService>(); + + var factory = new ConnectionFactory() + { + HostName = Configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; + + if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) + { + factory.UserName = Configuration["EventBusUserName"]; + } + + if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) + { + factory.Password = Configuration["EventBusPassword"]; + } + + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } private void ConfigureEventBus(IApplicationBuilder app) { - var eventBus = app.ApplicationServices.GetRequiredService(); + var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe(); } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs index 6b8ed53e4..f67661450 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -15,12 +15,12 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler { private readonly IMediator _mediator; - private readonly IEventBus _eventBus; + private readonly IMultiEventBus _eventBus; private readonly ILogger _logger; public UserCheckoutAcceptedIntegrationEventHandler( IMediator mediator, - ILogger logger, IEventBus eventBus) + ILogger logger, IMultiEventBus eventBus) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs index cb7ce5513..08fa966b3 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs @@ -19,13 +19,13 @@ namespace Ordering.API.Application.IntegrationEvents public class OrderingIntegrationEventService : IOrderingIntegrationEventService { private readonly Func _integrationEventLogServiceFactory; - private readonly IEventBus _eventBus; + private readonly IMultiEventBus _eventBus; private readonly OrderingContext _orderingContext; private readonly IntegrationEventLogContext _eventLogContext; private readonly IIntegrationEventLogService _eventLogService; private readonly ILogger _logger; - public OrderingIntegrationEventService(IEventBus eventBus, + public OrderingIntegrationEventService(IMultiEventBus eventBus, OrderingContext orderingContext, IntegrationEventLogContext eventLogContext, Func integrationEventLogServiceFactory, diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 9c4e25aea..d4fe19ea3 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -115,7 +115,7 @@ private void ConfigureEventBus(IApplicationBuilder app) { - var eventBus = app.ApplicationServices.GetRequiredService(); + var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe>(); eventBus.Subscribe>(); @@ -284,55 +284,47 @@ services.AddTransient(); - if (configuration.GetValue("AzureServiceBusEnabled")) + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp, configuration)); + connections.AddConnection(GenerateConnection("TenantB", sp, configuration)); + return connections; + }); - var factory = new ConnectionFactory() - { - HostName = configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; + return services; + } + + private static IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp, IConfiguration configuration) + { + var logger = sp.GetRequiredService>(); - if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) - { - factory.UserName = configuration["EventBusUserName"]; - } + var factory = new ConnectionFactory() + { + HostName = configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; - if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) - { - factory.Password = configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; + if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) + { + factory.UserName = configuration["EventBusUserName"]; + } + if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) + { + factory.Password = configuration["EventBusPassword"]; + } - var retryCount = 5; - if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } + factory.VirtualHost = vHost; - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); + var retryCount = 5; + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(configuration["EventBusRetryCount"]); } - return services; + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); } public static IServiceCollection AddCustomConfiguration(this IServiceCollection services, IConfiguration configuration) @@ -363,25 +355,12 @@ public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) { var subscriptionClientName = configuration["SubscriptionClientName"]; - -/* if (configuration.GetValue("AzureServiceBusEnabled")) + { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else*/ - { - services.AddSingleton(sp => + services.AddSingleton(sp => { - var rabbitMQPersistentConnection = sp.GetRequiredService(); + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); var iLifetimeScope = sp.GetRequiredService(); var logger = sp.GetRequiredService>(); var eventBusSubcriptionsManager = sp.GetRequiredService(); @@ -392,7 +371,17 @@ retryCount = int.Parse(configuration["EventBusRetryCount"]); } - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); + List eventBuses = new List(); + + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); }); } diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs b/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs index 408cf42cd..7d39ead70 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs +++ b/src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs @@ -15,6 +15,7 @@ using Ordering.BackgroundTasks.Configuration; using Ordering.BackgroundTasks.Tasks; using RabbitMQ.Client; using System; +using System.Collections.Generic; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; @@ -46,53 +47,16 @@ namespace Ordering.BackgroundTasks services.AddSingleton(); //configure event bus related services - - if (Configuration.GetValue("AzureServiceBusEnabled")) + + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp)); + connections.AddConnection(GenerateConnection("TenantB", sp)); - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } - - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + return connections; + }); + RegisterEventBus(services); @@ -104,6 +68,37 @@ namespace Ordering.BackgroundTasks return new AutofacServiceProvider(container.Build()); } + private IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp) + { + var logger = sp.GetRequiredService>(); + + var factory = new ConnectionFactory() + { + HostName = Configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; + + if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) + { + factory.UserName = Configuration["EventBusUserName"]; + } + + if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) + { + factory.Password = Configuration["EventBusPassword"]; + } + + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) @@ -124,38 +119,33 @@ namespace Ordering.BackgroundTasks private void RegisterEventBus(IServiceCollection services) { var subscriptionClientName = Configuration["SubscriptionClientName"]; - -/* if (Configuration.GetValue("AzureServiceBusEnabled")) + + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else*/ - { - services.AddSingleton(sp => + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); + var iLifetimeScope = sp.GetRequiredService(); + var logger = sp.GetRequiredService>(); + var eventBusSubcriptionsManager = sp.GetRequiredService(); + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); - }); - } + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + List eventBuses = new List(); + + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); + }); + services.AddSingleton(); } diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs b/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs index b667615fb..6f8282350 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs +++ b/src/Services/Ordering/Ordering.BackgroundTasks/Tasks/GracePeriodManagerTask.cs @@ -21,12 +21,12 @@ namespace Ordering.BackgroundTasks.Tasks { private readonly ILogger _logger; private readonly BackgroundTaskSettings _settings; - private readonly IEventBus _eventBus; + private readonly IMultiEventBus _eventBus; private static readonly String identityUrl = @"http://identity.api/"; public GracePeriodManagerService( IOptions settings, - IEventBus eventBus, + IMultiEventBus eventBus, ILogger logger) { _settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings)); diff --git a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs index 4929ffed0..f335f3986 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs @@ -16,6 +16,7 @@ using Ordering.SignalrHub.IntegrationEvents.EventHandling; using Ordering.SignalrHub.IntegrationEvents.Events; using RabbitMQ.Client; using System; +using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; @@ -42,10 +43,10 @@ namespace Ordering.SignalrHub { options.AddPolicy("CorsPolicy", builder => builder - .AllowAnyMethod() - .AllowAnyHeader() - .SetIsOriginAllowed((host) => true) - .AllowCredentials()); + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed((host) => true) + .AllowCredentials()); }); if (Configuration.GetValue("IsClusterEnv") == bool.TrueString) @@ -59,52 +60,14 @@ namespace Ordering.SignalrHub services.AddSignalR(); } - if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp)); + connections.AddConnection(GenerateConnection("TenantB", sp)); - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } - - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + return connections; + }); ConfigureAuthService(services); @@ -120,6 +83,38 @@ namespace Ordering.SignalrHub return new AutofacServiceProvider(container.Build()); } + private IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp) + { + var logger = sp.GetRequiredService>(); + + var factory = new ConnectionFactory() + { + HostName = Configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; + + if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) + { + factory.UserName = Configuration["EventBusUserName"]; + } + + if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) + { + factory.Password = Configuration["EventBusPassword"]; + } + + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { @@ -161,14 +156,25 @@ namespace Ordering.SignalrHub private void ConfigureEventBus(IApplicationBuilder app) { - var eventBus = app.ApplicationServices.GetRequiredService(); - - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); + var eventBus = app.ApplicationServices.GetRequiredService(); + + eventBus + .Subscribe(); + eventBus + .Subscribe(); + eventBus + .Subscribe(); + eventBus + .Subscribe(); + eventBus + .Subscribe(); + eventBus + .Subscribe(); } private void ConfigureAuthService(IServiceCollection services) @@ -182,7 +188,6 @@ namespace Ordering.SignalrHub { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddJwtBearer(options => { options.Authority = identityUrl; @@ -195,37 +200,33 @@ namespace Ordering.SignalrHub { var subscriptionClientName = Configuration["SubscriptionClientName"]; -/* if (Configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else*/ + services.AddSingleton(sp => { - services.AddSingleton(sp => + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); + var iLifetimeScope = sp.GetRequiredService(); + var logger = sp.GetRequiredService>(); + var eventBusSubcriptionsManager = sp.GetRequiredService(); + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); - }); - } + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + List eventBuses = new List(); + + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); + }); + services.AddSingleton(); } @@ -233,7 +234,8 @@ namespace Ordering.SignalrHub public static class CustomExtensionMethods { - public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, + IConfiguration configuration) { var hcBuilder = services.AddHealthChecks(); @@ -246,7 +248,7 @@ namespace Ordering.SignalrHub configuration["EventBusConnection"], topicName: "eshop_event_bus", name: "signalr-servicebus-check", - tags: new string[] { "servicebus" }); + tags: new string[] {"servicebus"}); } else { @@ -254,10 +256,10 @@ namespace Ordering.SignalrHub .AddRabbitMQ( $"amqp://{configuration["EventBusConnection"]}", name: "signalr-rabbitmqbus-check", - tags: new string[] { "rabbitmqbus" }); + tags: new string[] {"rabbitmqbus"}); } return services; } } -} +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs index c2f31b4cf..e8e81e082 100644 --- a/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs @@ -11,12 +11,12 @@ public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : IIntegrationEventHandler { - private readonly IEventBus _eventBus; + private readonly IMultiEventBus _eventBus; private readonly PaymentSettings _settings; private readonly ILogger _logger; public OrderStatusChangedToStockConfirmedIntegrationEventHandler( - IEventBus eventBus, + IMultiEventBus eventBus, IOptionsSnapshot settings, ILogger logger) { diff --git a/src/Services/Payment/Payment.API/Startup.cs b/src/Services/Payment/Payment.API/Startup.cs index 5692f6826..cdc7ebb63 100644 --- a/src/Services/Payment/Payment.API/Startup.cs +++ b/src/Services/Payment/Payment.API/Startup.cs @@ -16,6 +16,7 @@ using Payment.API.IntegrationEvents.EventHandling; using Payment.API.IntegrationEvents.Events; using RabbitMQ.Client; using System; +using System.Collections.Generic; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; @@ -38,52 +39,16 @@ namespace Payment.API services.Configure(Configuration); RegisterAppInsights(services); - - if (Configuration.GetValue("AzureServiceBusEnabled")) + + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } - - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp)); + connections.AddConnection(GenerateConnection("TenantB", sp)); + return connections; + }); + RegisterEventBus(services); var container = new ContainerBuilder(); @@ -91,6 +56,37 @@ namespace Payment.API return new AutofacServiceProvider(container.Build()); } + private IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp) + { + var logger = sp.GetRequiredService>(); + + var factory = new ConnectionFactory() + { + HostName = Configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; + + if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) + { + factory.UserName = Configuration["EventBusUserName"]; + } + + if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) + { + factory.Password = Configuration["EventBusPassword"]; + } + + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { @@ -140,37 +136,33 @@ namespace Payment.API { var subscriptionClientName = Configuration["SubscriptionClientName"]; -/* if (Configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else*/ + + services.AddSingleton(sp => { - services.AddSingleton(sp => + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); + var iLifetimeScope = sp.GetRequiredService(); + var logger = sp.GetRequiredService>(); + var eventBusSubcriptionsManager = sp.GetRequiredService(); + + var retryCount = 5; + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); - }); - } + retryCount = int.Parse(Configuration["EventBusRetryCount"]); + } + + List eventBuses = new List(); + + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); + }); + services.AddTransient(); services.AddSingleton(); @@ -178,7 +170,7 @@ namespace Payment.API private void ConfigureEventBus(IApplicationBuilder app) { - var eventBus = app.ApplicationServices.GetRequiredService(); + var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe(); } } diff --git a/src/Services/Webhooks/Webhooks.API/Startup.cs b/src/Services/Webhooks/Webhooks.API/Startup.cs index e54163bc6..8186f1cde 100644 --- a/src/Services/Webhooks/Webhooks.API/Startup.cs +++ b/src/Services/Webhooks/Webhooks.API/Startup.cs @@ -126,7 +126,7 @@ namespace Webhooks.API protected virtual void ConfigureEventBus(IApplicationBuilder app) { - var eventBus = app.ApplicationServices.GetRequiredService(); + var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe(); eventBus.Subscribe(); eventBus.Subscribe(); @@ -231,26 +231,10 @@ namespace Webhooks.API public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) { var subscriptionClientName = configuration["SubscriptionClientName"]; - -/* if (configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - - } - else*/ - { - services.AddSingleton(sp => + + services.AddSingleton(sp => { - var rabbitMQPersistentConnection = sp.GetRequiredService(); + var multiRabbitMqPersistentConnections = sp.GetRequiredService(); var iLifetimeScope = sp.GetRequiredService(); var logger = sp.GetRequiredService>(); var eventBusSubcriptionsManager = sp.GetRequiredService(); @@ -261,9 +245,19 @@ namespace Webhooks.API retryCount = int.Parse(configuration["EventBusRetryCount"]); } - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount); + List eventBuses = new List(); + + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[0], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantA", subscriptionClientName, retryCount)); + eventBuses.Add(new EventBusRabbitMQ(multiRabbitMqPersistentConnections.GetConnections()[1], logger, + iLifetimeScope, eventBusSubcriptionsManager, "TenantB", subscriptionClientName, retryCount)); + Dictionary tenants = new Dictionary(); + tenants.Add(1, "TenantA"); + tenants.Add(2, "TenantB"); + + return new MultiEventBusRabbitMQ(eventBuses, tenants); }); - } + services.AddSingleton(); services.AddTransient(); @@ -304,53 +298,52 @@ namespace Webhooks.API { services.AddTransient>( sp => (DbConnection c) => new IntegrationEventLogService(c)); - - if (configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - var serviceBusConnection = new ServiceBusConnectionStringBuilder(configuration["EventBusConnection"]); - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - else + + + services.AddSingleton(sp => { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); + IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections(); + connections.AddConnection(GenerateConnection("TenantA", sp, configuration)); + connections.AddConnection(GenerateConnection("TenantB", sp, configuration)); - var factory = new ConnectionFactory() - { - HostName = configuration["EventBusConnection"], - DispatchConsumersAsync = true - }; + return connections; + }); + - if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) - { - factory.UserName = configuration["EventBusUserName"]; - } + return services; + } - if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) - { - factory.Password = configuration["EventBusPassword"]; - } - - factory.VirtualHost = "TenantA"; + private static IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp, IConfiguration configuration) + { + var logger = sp.GetRequiredService>(); - var retryCount = 5; - if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } + var factory = new ConnectionFactory() + { + HostName = configuration["EventBusConnection"], + DispatchConsumersAsync = true + }; - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); + if (!string.IsNullOrEmpty(configuration["EventBusUserName"])) + { + factory.UserName = configuration["EventBusUserName"]; } - return services; - } + if (!string.IsNullOrEmpty(configuration["EventBusPassword"])) + { + factory.Password = configuration["EventBusPassword"]; + } + factory.VirtualHost = vHost; + + var retryCount = 5; + if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) + { + retryCount = int.Parse(configuration["EventBusRetryCount"]); + } + + return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); + } + public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) { // prevent from mapping "sub" claim to nameidentifier.