Skip to content

Commit 172367d

Browse files
committed
Connect to the rabbitmq container using settings.
1 parent 4d1269b commit 172367d

7 files changed

Lines changed: 22 additions & 19 deletions

File tree

docker-compose.override.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
- ASPNETCORE_URLS=http://0.0.0.0:5103
99
- ConnectionString=basket.data
1010
- identityUrl=http://identity.api:5105 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
11+
- EventBusConnection=rabbitmq
1112
ports:
1213
- "5103:5103"
1314

@@ -17,6 +18,7 @@ services:
1718
- ASPNETCORE_URLS=http://0.0.0.0:5101
1819
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word
1920
- ExternalCatalogBaseUrl=http://localhost:5101 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
21+
- EventBusConnection=rabbitmq
2022
ports:
2123
- "5101:5101"
2224

@@ -36,6 +38,7 @@ services:
3638
- ASPNETCORE_URLS=http://0.0.0.0:5102
3739
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word
3840
- identityUrl=http://identity.api:5105 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
41+
- EventBusConnection=rabbitmq
3942
ports:
4043
- "5102:5102"
4144

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
88
public class BasketSettings
99
{
1010
public string ConnectionString { get; set; }
11+
12+
public string EventBusConnection { get; set; }
1113
}
1214
}

src/Services/Basket/Basket.API/Events/CatalogPriceChangedHandler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public async Task Handle(CatalogPriceChanged @event)
2828

2929
private async Task UpdateBasket(int itemId, decimal newPrice, CustomerBasket basket)
3030
{
31-
//TODO here seems to be a problem with the format
32-
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.Id) == itemId).ToList();
31+
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == itemId).ToList();
3332
if (itemsToUpdate != null)
3433
{
3534
foreach (var item in itemsToUpdate)
@@ -39,7 +38,7 @@ private async Task UpdateBasket(int itemId, decimal newPrice, CustomerBasket bas
3938
item.OldUnitPrice = originalPrice;
4039
}
4140
await _repository.UpdateBasket(basket);
42-
}
41+
}
4342
}
4443
}
4544
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,15 @@ public void ConfigureServices(IServiceCollection services)
8181
services.AddTransient<IBasketRepository, RedisBasketRepository>();
8282
services.AddTransient<IIntegrationEventHandler<CatalogPriceChanged>, CatalogPriceChangedHandler>();
8383

84-
var eventBus = new EventBus();
84+
var serviceProvider = services.BuildServiceProvider();
85+
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
86+
var eventBus = new EventBus(configuration.EventBusConnection);
8587
services.AddSingleton<IEventBus>(eventBus);
8688

87-
var serviceProvider = services.BuildServiceProvider();
89+
8890
var catalogPriceHandler = serviceProvider.GetService<IIntegrationEventHandler<CatalogPriceChanged>>();
8991
eventBus.Subscribe<CatalogPriceChanged>(catalogPriceHandler);
90-
91-
//var container = new ContainerBuilder();
92-
//container.Populate(services);
93-
//container.RegisterModule(new ApplicationModule());
94-
95-
//return new AutofacServiceProvider(container.Build());
96-
97-
//var eventBus = new EventBus();
98-
//services.AddSingleton<IEventBus>(eventBus);
99-
//eventBus.Subscribe<CatalogPriceChanged>(new CatalogPriceChangedHandler(new RedisBasketRepository());
92+
10093
}
10194

10295
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Extensions.Configuration;
1212
using Microsoft.Extensions.DependencyInjection;
1313
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Options;
1415
using System;
1516
using System.IO;
1617
using System.Reflection;
@@ -74,7 +75,9 @@ public void ConfigureServices(IServiceCollection services)
7475
.AllowCredentials());
7576
});
7677

77-
services.AddSingleton<IEventBus, EventBus>();
78+
var serviceProvider = services.BuildServiceProvider();
79+
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<Settings>>().Value;
80+
services.AddSingleton<IEventBus>(new EventBus(configuration.EventBusConnection));
7881

7982
services.AddMvc();
8083
}

src/Services/Catalog/Catalog.API/settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
99
public class Settings
1010
{
1111
public string ExternalCatalogBaseUrl {get;set;}
12+
public string EventBusConnection { get; set; }
1213
}
1314
}

src/Services/Common/Infrastructure/EventBus.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure
1616
public class EventBus : IEventBus
1717
{
1818
private readonly string _brokerName = "event_bus";
19+
private readonly string _connectionString;
1920
private readonly Dictionary<string, List<IIntegrationEventHandler>> _handlers;
2021
private readonly List<Type> _eventTypes;
2122

2223
private Tuple<IModel, IConnection> _connection;
2324
private string _queueName;
2425

2526

26-
public EventBus()
27+
public EventBus(string connectionString)
2728
{
29+
_connectionString = connectionString;
2830
_handlers = new Dictionary<string, List<IIntegrationEventHandler>>();
2931
_eventTypes = new List<Type>();
3032
}
3133
public void Publish(IIntegrationEvent @event)
3234
{
3335
var eventName = @event.GetType().Name;
34-
var factory = new ConnectionFactory() { HostName = "172.20.0.1" };
36+
var factory = new ConnectionFactory() { HostName = _connectionString };
3537
using (var connection = factory.CreateConnection())
3638
using (var channel = connection.CreateModel())
3739
{
@@ -105,7 +107,7 @@ private IModel GetChannel()
105107
}
106108
else
107109
{
108-
var factory = new ConnectionFactory() { HostName = "172.20.0.1" };
110+
var factory = new ConnectionFactory() { HostName = _connectionString };
109111
var connection = factory.CreateConnection();
110112
var channel = connection.CreateModel();
111113

0 commit comments

Comments
 (0)