|
4 | 4 | using global::Catalog.API.IntegrationEvents; |
5 | 5 | using Microsoft.AspNetCore.Builder; |
6 | 6 | using Microsoft.AspNetCore.Hosting; |
| 7 | + using Microsoft.Azure.ServiceBus; |
7 | 8 | using Microsoft.EntityFrameworkCore; |
8 | 9 | using Microsoft.EntityFrameworkCore.Infrastructure; |
9 | 10 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; |
10 | 11 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
11 | 12 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; |
| 13 | + using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; |
12 | 14 | using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; |
13 | 15 | using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; |
14 | 16 | using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
@@ -109,20 +111,34 @@ public void ConfigureServices(IServiceCollection services) |
109 | 111 |
|
110 | 112 | services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>(); |
111 | 113 |
|
112 | | - services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
| 114 | + if (Configuration.GetValue<bool>("AzureServiceBus")) |
113 | 115 | { |
114 | | - var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value; |
115 | | - var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
116 | | - var factory = new ConnectionFactory() |
| 116 | + services.AddSingleton<IServiceBusPersisterConnection>(sp => |
117 | 117 | { |
118 | | - HostName = settings.EventBusConnection |
119 | | - }; |
| 118 | + var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value; |
| 119 | + var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
120 | 120 |
|
121 | | - return new DefaultRabbitMQPersistentConnection(factory, logger); |
122 | | - }); |
| 121 | + var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.ServiceBusConnection); |
123 | 122 |
|
124 | | - services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
125 | | - services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
| 123 | + return new DefaultServiceBusPersisterConnection(serviceBusConnection, TimeSpan.FromSeconds(5), RetryPolicy.Default, logger); |
| 124 | + }); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
| 129 | + { |
| 130 | + var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value; |
| 131 | + var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
| 132 | + var factory = new ConnectionFactory() |
| 133 | + { |
| 134 | + HostName = settings.EventBusConnection |
| 135 | + }; |
| 136 | + |
| 137 | + return new DefaultRabbitMQPersistentConnection(factory, logger); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + RegisterServiceBus(services); |
126 | 142 | } |
127 | 143 |
|
128 | 144 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
@@ -179,5 +195,28 @@ private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFac |
179 | 195 | ctx.Database.CloseConnection(); |
180 | 196 | } |
181 | 197 | } |
| 198 | + |
| 199 | + private void RegisterServiceBus(IServiceCollection services) |
| 200 | + { |
| 201 | + if (Configuration.GetValue<bool>("AzureServiceBus")) |
| 202 | + { |
| 203 | + services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
| 204 | + { |
| 205 | + var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
| 206 | + var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
| 207 | + var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
| 208 | + var subscriptionClientName = "Catalog"; |
| 209 | + |
| 210 | + return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
| 211 | + eventBusSubcriptionsManager, subscriptionClientName); |
| 212 | + }); |
| 213 | + } |
| 214 | + else |
| 215 | + { |
| 216 | + services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
| 217 | + } |
| 218 | + |
| 219 | + services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
| 220 | + } |
182 | 221 | } |
183 | 222 | } |
0 commit comments