|
12 | 12 | using Infrastructure.Services; |
13 | 13 | using Microsoft.AspNetCore.Builder; |
14 | 14 | using Microsoft.AspNetCore.Hosting; |
| 15 | + using Microsoft.Azure.ServiceBus; |
15 | 16 | using Microsoft.EntityFrameworkCore; |
16 | 17 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; |
17 | 18 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
18 | 19 | using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; |
| 20 | + using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; |
19 | 21 | using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; |
20 | 22 | using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; |
21 | 23 | using Microsoft.Extensions.Configuration; |
@@ -113,20 +115,33 @@ public IServiceProvider ConfigureServices(IServiceCollection services) |
113 | 115 | var serviceProvider = services.BuildServiceProvider(); |
114 | 116 | services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>(); |
115 | 117 |
|
116 | | - services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
| 118 | + if (Configuration.GetValue<bool>("AzureServiceBus")) |
117 | 119 | { |
118 | | - var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
| 120 | + services.AddSingleton<IServiceBusPersisterConnection>(sp => |
| 121 | + { |
| 122 | + var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
| 123 | + |
| 124 | + var serviceBusConnection = new ServiceBusConnectionStringBuilder(Configuration["ServiceBusConnection"]); |
119 | 125 |
|
120 | | - var factory = new ConnectionFactory() |
| 126 | + return new DefaultServiceBusPersisterConnection(serviceBusConnection, TimeSpan.FromSeconds(5), RetryPolicy.Default, logger); |
| 127 | + }); |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
121 | 132 | { |
122 | | - HostName = Configuration["EventBusConnection"] |
123 | | - }; |
| 133 | + var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
124 | 134 |
|
125 | | - return new DefaultRabbitMQPersistentConnection(factory, logger); |
126 | | - }); |
| 135 | + var factory = new ConnectionFactory() |
| 136 | + { |
| 137 | + HostName = Configuration["EventBusConnection"] |
| 138 | + }; |
127 | 139 |
|
128 | | - services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
129 | | - services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
| 140 | + return new DefaultRabbitMQPersistentConnection(factory, logger); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + RegisterServiceBus(services); |
130 | 145 |
|
131 | 146 | services.AddOptions(); |
132 | 147 |
|
@@ -174,5 +189,28 @@ protected virtual void ConfigureAuth(IApplicationBuilder app) |
174 | 189 | RequireHttpsMetadata = false |
175 | 190 | }); |
176 | 191 | } |
| 192 | + |
| 193 | + private void RegisterServiceBus(IServiceCollection services) |
| 194 | + { |
| 195 | + if (Configuration.GetValue<bool>("AzureServiceBus")) |
| 196 | + { |
| 197 | + services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
| 198 | + { |
| 199 | + var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
| 200 | + var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
| 201 | + var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
| 202 | + var subscriptionClientName = "Ordering"; |
| 203 | + |
| 204 | + return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
| 205 | + eventBusSubcriptionsManager, subscriptionClientName); |
| 206 | + }); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
| 211 | + } |
| 212 | + |
| 213 | + services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
| 214 | + } |
177 | 215 | } |
178 | 216 | } |
0 commit comments