diff --git a/src/Services/Marketing/Marketing.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs b/src/Services/Marketing/Marketing.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs index b73e7b659..9da64aa2e 100644 --- a/src/Services/Marketing/Marketing.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs +++ b/src/Services/Marketing/Marketing.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs @@ -1,10 +1,9 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Events { using Marketing.API.Model; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using System.Collections.Generic; - public class UserLocationUpdatedIntegrationEvent : IntegrationEvent + public class UserLocationUpdatedIntegrationEvent { public string UserId { get; set; } diff --git a/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs b/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs index 3d5e62e45..534c62890 100644 --- a/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs +++ b/src/Services/Marketing/Marketing.API/IntegrationEvents/Handlers/UserLocationUpdatedIntegrationEventHandler.cs @@ -1,8 +1,9 @@ -namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Handlers +using DotNetCore.CAP; + +namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Handlers { using Marketing.API.IntegrationEvents.Events; using Marketing.API.Model; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Repositories; using Microsoft.Extensions.Logging; using Serilog.Context; @@ -10,8 +11,7 @@ using System.Collections.Generic; using System.Threading.Tasks; - public class UserLocationUpdatedIntegrationEventHandler - : IIntegrationEventHandler + public class UserLocationUpdatedIntegrationEventHandler : ICapSubscribe { private readonly IMarketingDataRepository _marketingDataRepository; private readonly ILogger _logger; @@ -24,11 +24,12 @@ _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + //TODO: [CapSubscribe(nameof(UserLocationUpdatedIntegrationEvent))] public async Task Handle(UserLocationUpdatedIntegrationEvent @event) { - using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}")) + using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}")) { - _logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event); + _logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event); var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId); userMarketingData = userMarketingData ?? diff --git a/src/Services/Marketing/Marketing.API/Marketing.API.csproj b/src/Services/Marketing/Marketing.API/Marketing.API.csproj index 373bcb0a6..bfba7d0d4 100644 --- a/src/Services/Marketing/Marketing.API/Marketing.API.csproj +++ b/src/Services/Marketing/Marketing.API/Marketing.API.csproj @@ -27,6 +27,10 @@ + + + + @@ -36,10 +40,10 @@ - - - - + + + + @@ -48,8 +52,6 @@ - - diff --git a/src/Services/Marketing/Marketing.API/Startup.cs b/src/Services/Marketing/Marketing.API/Startup.cs index 75c749452..8c1822bec 100644 --- a/src/Services/Marketing/Marketing.API/Startup.cs +++ b/src/Services/Marketing/Marketing.API/Startup.cs @@ -5,11 +5,6 @@ using AspNetCore.Http; using Autofac; using Autofac.Extensions.DependencyInjection; - using Azure.ServiceBus; - using BuildingBlocks.EventBus; - using BuildingBlocks.EventBus.Abstractions; - using BuildingBlocks.EventBusRabbitMQ; - using BuildingBlocks.EventBusServiceBus; using EntityFrameworkCore; using Extensions.Configuration; using Extensions.DependencyInjection; @@ -19,7 +14,6 @@ using Infrastructure.Filters; using Infrastructure.Repositories; using Infrastructure.Services; - using IntegrationEvents.Events; using Marketing.API.IntegrationEvents.Handlers; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.ServiceFabric; @@ -81,49 +75,48 @@ //Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval }); - if (Configuration.GetValue("AzureServiceBusEnabled")) + services.AddTransient(); + + 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["MongoConnectionString"]; + configure.DatabaseName = Configuration["MongoDatabase"]; }); - } - 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"]); + } + if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"])) + { + options.DefaultGroup = Configuration["SubscriptionClientName"]; + } + }); + // Add framework services. services.AddSwaggerGen(options => { @@ -159,9 +152,7 @@ .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); - }); - - RegisterEventBus(services); + }); services.AddTransient(); services.AddSingleton(); @@ -212,9 +203,7 @@ c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Marketing.API V1"); c.OAuthClientId("marketingswaggerui"); c.OAuthAppName("Marketing Swagger UI"); - }); - - ConfigureEventBus(app); + }); } private void RegisterAppInsights(IServiceCollection services) @@ -253,52 +242,6 @@ }); } - 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(); - services.AddTransient(); - } - - private void ConfigureEventBus(IApplicationBuilder app) - { - var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe(); - } - protected virtual void ConfigureAuth(IApplicationBuilder app) { if (Configuration.GetValue("UseLoadTest"))