Skip to content

Commit 721a4fd

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

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

src/Services/Basket/Basket.API/Basket.API.csproj

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

4242
<ItemGroup>
4343
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
44+
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
4445
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
4546
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" />
4647
<ProjectReference Include="..\..\..\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" />

src/Services/Basket/Basket.API/BasketSettings.cs

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

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

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Net;
2222
using System.Threading.Tasks;
2323
using System;
24+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
25+
using Microsoft.Azure.ServiceBus;
2426

2527
namespace Microsoft.eShopOnContainers.Services.Basket.API
2628
{
@@ -70,17 +72,33 @@ public void ConfigureServices(IServiceCollection services)
7072
});
7173

7274

73-
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
75+
if (Configuration.GetValue<bool>("AzureServiceBus"))
7476
{
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 =>
7878
{
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+
}
81101

82-
return new DefaultRabbitMQPersistentConnection(factory, logger);
83-
});
84102

85103
services.AddSwaggerGen();
86104

@@ -113,7 +131,24 @@ public void ConfigureServices(IServiceCollection services)
113131

114132
private void RegisterServiceBus(IServiceCollection services)
115133
{
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+
117152
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
118153

119154
services.AddTransient<ProductPriceChangedIntegrationEventHandler>();

src/Services/Basket/Basket.API/appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
}
99
},
1010
"IdentityUrl": "http://localhost:5105",
11-
"ConnectionString": "127.0.0.1"
11+
"ConnectionString": "127.0.0.1",
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)