diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 4e24592cf..2fe32722e 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -8,6 +8,7 @@ services: - ASPNETCORE_URLS=http://0.0.0.0:5103 - ConnectionString=basket.data - identityUrl=http://identity.api:5105 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105. + - EventBusConnection=rabbitmq ports: - "5103:5103" @@ -17,6 +18,7 @@ services: - ASPNETCORE_URLS=http://0.0.0.0:5101 - ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word - ExternalCatalogBaseUrl=http://localhost:5101 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105. + - EventBusConnection=rabbitmq ports: - "5101:5101" @@ -36,6 +38,7 @@ services: - ASPNETCORE_URLS=http://0.0.0.0:5102 - ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word - identityUrl=http://identity.api:5105 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105. + - EventBusConnection=rabbitmq ports: - "5102:5102" diff --git a/src/Services/Basket/Basket.API/BasketSettings.cs b/src/Services/Basket/Basket.API/BasketSettings.cs index d584c102f..6aae45015 100644 --- a/src/Services/Basket/Basket.API/BasketSettings.cs +++ b/src/Services/Basket/Basket.API/BasketSettings.cs @@ -8,5 +8,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API public class BasketSettings { public string ConnectionString { get; set; } + + public string EventBusConnection { get; set; } } } diff --git a/src/Services/Basket/Basket.API/Events/CatalogPriceChangedHandler.cs b/src/Services/Basket/Basket.API/Events/CatalogPriceChangedHandler.cs index db116b0c4..006c82119 100644 --- a/src/Services/Basket/Basket.API/Events/CatalogPriceChangedHandler.cs +++ b/src/Services/Basket/Basket.API/Events/CatalogPriceChangedHandler.cs @@ -28,8 +28,7 @@ namespace Basket.API.Events private async Task UpdateBasket(int itemId, decimal newPrice, CustomerBasket basket) { - //TODO here seems to be a problem with the format - var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.Id) == itemId).ToList(); + var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == itemId).ToList(); if (itemsToUpdate != null) { foreach (var item in itemsToUpdate) @@ -39,7 +38,7 @@ namespace Basket.API.Events item.OldUnitPrice = originalPrice; } await _repository.UpdateBasket(basket); - } + } } } } diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index f7fa06df8..e0bc9c076 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -81,22 +81,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API services.AddTransient(); services.AddTransient, CatalogPriceChangedHandler>(); - var eventBus = new EventBus(); + var serviceProvider = services.BuildServiceProvider(); + var configuration = serviceProvider.GetRequiredService>().Value; + var eventBus = new EventBus(configuration.EventBusConnection); services.AddSingleton(eventBus); - var serviceProvider = services.BuildServiceProvider(); + var catalogPriceHandler = serviceProvider.GetService>(); eventBus.Subscribe(catalogPriceHandler); - - //var container = new ContainerBuilder(); - //container.Populate(services); - //container.RegisterModule(new ApplicationModule()); - - //return new AutofacServiceProvider(container.Build()); - - //var eventBus = new EventBus(); - //services.AddSingleton(eventBus); - //eventBus.Subscribe(new CatalogPriceChangedHandler(new RedisBasketRepository()); + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index c8ecf72fd..9e0da8d00 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -11,6 +11,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; using System; using System.IO; using System.Reflection; @@ -74,7 +75,9 @@ .AllowCredentials()); }); - services.AddSingleton(); + var serviceProvider = services.BuildServiceProvider(); + var configuration = serviceProvider.GetRequiredService>().Value; + services.AddSingleton(new EventBus(configuration.EventBusConnection)); services.AddMvc(); } diff --git a/src/Services/Catalog/Catalog.API/settings.cs b/src/Services/Catalog/Catalog.API/settings.cs index 76091c409..a6e959552 100644 --- a/src/Services/Catalog/Catalog.API/settings.cs +++ b/src/Services/Catalog/Catalog.API/settings.cs @@ -9,5 +9,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API public class Settings { public string ExternalCatalogBaseUrl {get;set;} + public string EventBusConnection { get; set; } } } diff --git a/src/Services/Common/Infrastructure/EventBus.cs b/src/Services/Common/Infrastructure/EventBus.cs index dd273d571..47e51d28d 100644 --- a/src/Services/Common/Infrastructure/EventBus.cs +++ b/src/Services/Common/Infrastructure/EventBus.cs @@ -16,6 +16,7 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure public class EventBus : IEventBus { private readonly string _brokerName = "event_bus"; + private readonly string _connectionString; private readonly Dictionary> _handlers; private readonly List _eventTypes; @@ -23,15 +24,16 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure private string _queueName; - public EventBus() + public EventBus(string connectionString) { + _connectionString = connectionString; _handlers = new Dictionary>(); _eventTypes = new List(); } public void Publish(IIntegrationEvent @event) { var eventName = @event.GetType().Name; - var factory = new ConnectionFactory() { HostName = "172.20.0.1" }; + var factory = new ConnectionFactory() { HostName = _connectionString }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { @@ -105,7 +107,7 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure } else { - var factory = new ConnectionFactory() { HostName = "172.20.0.1" }; + var factory = new ConnectionFactory() { HostName = _connectionString }; var connection = factory.CreateConnection(); var channel = connection.CreateModel();