|
21 | 21 | using System.Net; |
22 | 22 | using System.Threading.Tasks; |
23 | 23 | using System; |
| 24 | +using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; |
| 25 | +using Microsoft.Azure.ServiceBus; |
24 | 26 |
|
25 | 27 | namespace Microsoft.eShopOnContainers.Services.Basket.API |
26 | 28 | { |
@@ -70,17 +72,33 @@ public void ConfigureServices(IServiceCollection services) |
70 | 72 | }); |
71 | 73 |
|
72 | 74 |
|
73 | | - services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
| 75 | + if (Configuration.GetValue<bool>("AzureServiceBus")) |
74 | 76 | { |
75 | | - var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value; |
76 | | - var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
77 | | - var factory = new ConnectionFactory() |
| 77 | + services.AddSingleton<IServiceBusPersisterConnection>(sp => |
78 | 78 | { |
79 | | - HostName = settings.EventBusConnection |
80 | | - }; |
| 79 | + var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value; |
| 80 | + var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
| 81 | + |
| 82 | + var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.ServiceBusConnection); |
| 83 | + |
| 84 | + return new DefaultServiceBusPersisterConnection(serviceBusConnection, TimeSpan.FromSeconds(5) , RetryPolicy.Default,logger); |
| 85 | + }); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
| 90 | + { |
| 91 | + var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value; |
| 92 | + var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
| 93 | + var factory = new ConnectionFactory() |
| 94 | + { |
| 95 | + HostName = settings.EventBusConnection |
| 96 | + }; |
| 97 | + |
| 98 | + return new DefaultRabbitMQPersistentConnection(factory, logger); |
| 99 | + }); |
| 100 | + } |
81 | 101 |
|
82 | | - return new DefaultRabbitMQPersistentConnection(factory, logger); |
83 | | - }); |
84 | 102 |
|
85 | 103 | services.AddSwaggerGen(); |
86 | 104 |
|
@@ -113,7 +131,24 @@ public void ConfigureServices(IServiceCollection services) |
113 | 131 |
|
114 | 132 | private void RegisterServiceBus(IServiceCollection services) |
115 | 133 | { |
116 | | - services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
| 134 | + if (Configuration.GetValue<bool>("AzureServiceBus")) |
| 135 | + { |
| 136 | + services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
| 137 | + { |
| 138 | + var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
| 139 | + var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
| 140 | + var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
| 141 | + var subscriptionClientName = "Basket"; |
| 142 | + |
| 143 | + return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
| 144 | + eventBusSubcriptionsManager, subscriptionClientName); |
| 145 | + }); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + services.AddSingleton<IEventBus, EventBusRabbitMQ>(); |
| 150 | + } |
| 151 | + |
117 | 152 | services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
118 | 153 |
|
119 | 154 | services.AddTransient<ProductPriceChangedIntegrationEventHandler>(); |
|
0 commit comments