diff --git a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs index f6b9ed708..d658249e7 100644 --- a/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs +++ b/src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs @@ -1,6 +1,7 @@ -namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services +using DotNetCore.CAP; + +namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services { - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories; using Microsoft.eShopOnContainers.Services.Locations.API.IntegrationEvents.Events; @@ -14,12 +15,12 @@ public class LocationsService : ILocationsService { private readonly ILocationsRepository _locationsRepository; - private readonly IEventBus _eventBus; + private readonly ICapPublisher _eventBus; private readonly ILogger _logger; public LocationsService( ILocationsRepository locationsRepository, - IEventBus eventBus, + ICapPublisher eventBus, ILogger logger) { _locationsRepository = locationsRepository ?? throw new ArgumentNullException(nameof(locationsRepository)); @@ -73,9 +74,9 @@ var newUserLocations = MapUserLocationDetails(newLocations); var @event = new UserLocationUpdatedIntegrationEvent(userId, newUserLocations); - _logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Publishing integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); - _eventBus.Publish(@event); + _eventBus.Publish(nameof(UserLocationUpdatedIntegrationEvent), @event); } private List MapUserLocationDetails(List newLocations) diff --git a/src/Services/Location/Locations.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs b/src/Services/Location/Locations.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs index 7c3e72769..183b8a73b 100644 --- a/src/Services/Location/Locations.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs +++ b/src/Services/Location/Locations.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs @@ -1,12 +1,12 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API.IntegrationEvents.Events { using Locations.API.Model; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using System.Collections.Generic; - public class UserLocationUpdatedIntegrationEvent : IntegrationEvent + public class UserLocationUpdatedIntegrationEvent { public string UserId { get; set; } + public List LocationList { get; set; } public UserLocationUpdatedIntegrationEvent(string userId, List locationList) diff --git a/src/Services/Location/Locations.API/Locations.API.csproj b/src/Services/Location/Locations.API/Locations.API.csproj index 9784c1f4d..6f7ccadf6 100644 --- a/src/Services/Location/Locations.API/Locations.API.csproj +++ b/src/Services/Location/Locations.API/Locations.API.csproj @@ -11,6 +11,10 @@ + + + + @@ -20,22 +24,16 @@ - - - - + + + + - - - - - - - + diff --git a/src/Services/Location/Locations.API/Startup.cs b/src/Services/Location/Locations.API/Startup.cs index 6d5fe3200..927caf2b0 100644 --- a/src/Services/Location/Locations.API/Startup.cs +++ b/src/Services/Location/Locations.API/Startup.cs @@ -9,11 +9,6 @@ using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.ServiceBus; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Middlewares; @@ -57,50 +52,45 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API services.Configure(Configuration); - if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddCap(options => { - services.AddSingleton(sp => + // using MongoDB as the event storage + options.UseMongoDB(configure => { - var logger = sp.GetRequiredService>(); - - var serviceBusConnectionString = Configuration["EventBusConnection"]; - var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); - - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); + configure.DatabaseConnection = Configuration["ConnectionString"]; + configure.DatabaseName= Configuration["Database"]; }); - } - else - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - - var factory = new ConnectionFactory() - { - HostName = Configuration["EventBusConnection"] - }; - - if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) - { - factory.UserName = Configuration["EventBusUserName"]; - } - if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) - { - factory.Password = Configuration["EventBusPassword"]; - } - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + if (Configuration.GetValue("AzureServiceBusEnabled")) + { + options.UseAzureServiceBus(Configuration["EventBusConnection"]); + } + else + { + options.UseRabbitMQ(conf => { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } + conf.HostName = Configuration["EventBusConnection"]; + if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) + { + conf.UserName = Configuration["EventBusUserName"]; + } + if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) + { + conf.Password = Configuration["EventBusPassword"]; + } + }); + } + + if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) + { + options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]); + } - RegisterEventBus(services); + if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"])) + { + options.DefaultGroup = Configuration["SubscriptionClientName"]; + } + }); // Add framework services. services.AddSwaggerGen(options => @@ -238,45 +228,6 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API app.UseAuthentication(); } - - private void RegisterEventBus(IServiceCollection services) - { - var subscriptionClientName = Configuration["SubscriptionClientName"]; - - if (Configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var serviceBusPersisterConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - return new EventBusServiceBus(serviceBusPersisterConnection, logger, - eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); - }); - } - else - { - services.AddSingleton(sp => - { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) - { - retryCount = int.Parse(Configuration["EventBusRetryCount"]); - } - - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); - }); - } - - services.AddSingleton(); - } } public static class CustomExtensionMethods