Skip to content

Commit 3acd556

Browse files
committed
- Declare new ServiceBus instance with "AzureServiceBus" boolean condition DI
- Add ServiceBusConnection and AzureServiceBus variables in settings - Add EventBusServiceBus dependencies
1 parent 7980c26 commit 3acd556

4 files changed

Lines changed: 55 additions & 11 deletions

File tree

src/Services/Catalog/Catalog.API/Catalog.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
<ItemGroup>
5959
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
60+
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
6061
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
6162
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" />
6263
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks.SqlServer\Microsoft.Extensions.HealthChecks.SqlServer.csproj" />

src/Services/Catalog/Catalog.API/CatalogSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public class CatalogSettings
55
public string ExternalCatalogBaseUrl {get;set;}
66

77
public string EventBusConnection { get; set; }
8+
9+
public string ServiceBusConnection { get; set; }
810
}
911
}

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
using global::Catalog.API.IntegrationEvents;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.Azure.ServiceBus;
78
using Microsoft.EntityFrameworkCore;
89
using Microsoft.EntityFrameworkCore.Infrastructure;
910
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
1011
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
1112
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
13+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
1214
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
1315
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
1416
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
@@ -109,20 +111,34 @@ public void ConfigureServices(IServiceCollection services)
109111

110112
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
111113

112-
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
114+
if (Configuration.GetValue<bool>("AzureServiceBus"))
113115
{
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 =>
117117
{
118-
HostName = settings.EventBusConnection
119-
};
118+
var settings = sp.GetRequiredService<IOptions<CatalogSettings>>().Value;
119+
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
120120

121-
return new DefaultRabbitMQPersistentConnection(factory, logger);
122-
});
121+
var serviceBusConnection = new ServiceBusConnectionStringBuilder(settings.ServiceBusConnection);
123122

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);
126142
}
127143

128144
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
@@ -179,5 +195,28 @@ private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFac
179195
ctx.Database.CloseConnection();
180196
}
181197
}
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+
}
182221
}
183222
}

src/Services/Catalog/Catalog.API/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
"System": "Information",
99
"Microsoft": "Information"
1010
}
11-
}
11+
},
12+
"ServiceBusConnection": "Endpoint=sb://eshoponazuretest.servicebus.windows.net/;SharedAccessKeyName=Root;SharedAccessKey=Xo9wlr4bRv5iqTTditgFhTeZqxIpczaAUqfspo+QE/s=;EntityPath=eshop_event_bus",
13+
"AzureServiceBus": "true"
1214
}

0 commit comments

Comments
 (0)