|
|
@ -4,11 +4,13 @@ |
|
|
|
using global::Catalog.API.IntegrationEvents; |
|
|
|
using Microsoft.AspNetCore.Builder; |
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
using Microsoft.Azure.ServiceBus; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
|
|
@ -109,20 +111,34 @@ |
|
|
|
|
|
|
|
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>(); |
|
|
|
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBus")) |
|
|
|
{ |
|
|
|
var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value; |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
|
|
|
var factory = new ConnectionFactory() |
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp => |
|
|
|
{ |
|
|
|
HostName = settings.EventBusConnection |
|
|
|
}; |
|
|
|
var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value; |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger); |
|
|
|
}); |
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.ServiceBusConnection); |
|
|
|
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, TimeSpan.FromSeconds(5), RetryPolicy.Default, logger); |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
|
|
|
{ |
|
|
|
var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value; |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
|
|
|
var factory = new ConnectionFactory() |
|
|
|
{ |
|
|
|
HostName = settings.EventBusConnection |
|
|
|
}; |
|
|
|
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
RegisterServiceBus(services); |
|
|
|
} |
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
|
|
@ -179,5 +195,28 @@ |
|
|
|
ctx.Database.CloseConnection(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void RegisterServiceBus(IServiceCollection services) |
|
|
|
{ |
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBus")) |
|
|
|
{ |
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
|
|
|
{ |
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
var subscriptionClientName = "Catalog"; |
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
|
|
|
eventBusSubcriptionsManager, subscriptionClientName); |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
|
|
|
} |
|
|
|
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |