diff --git a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEvent.cs b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEvent.cs index c4adda56e..ab072d8bc 100644 --- a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEvent.cs +++ b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEvent.cs @@ -1,12 +1,8 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; namespace Webhooks.API.IntegrationEvents { - public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToPaidIntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } diff --git a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEventHandler.cs b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEventHandler.cs index 2503bdca7..c8d609a30 100644 --- a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEventHandler.cs +++ b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToPaidIntegrationEventHandler.cs @@ -1,15 +1,13 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using System; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Threading.Tasks; +using DotNetCore.CAP; using Webhooks.API.Model; using Webhooks.API.Services; using Microsoft.Extensions.Logging; namespace Webhooks.API.IntegrationEvents { - public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler + public class OrderStatusChangedToPaidIntegrationEventHandler : ICapSubscribe { private readonly IWebhooksRetriever _retriever; private readonly IWebhooksSender _sender; @@ -21,6 +19,7 @@ namespace Webhooks.API.IntegrationEvents _logger = logger; } + //TODO [CapSubscribe(nameof(OrderStatusChangedToPaidIntegrationEvent))] public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event) { var subscriptions = await _retriever.GetSubscriptionsOfType(WebhookType.OrderPaid); diff --git a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEvent.cs b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEvent.cs index 10693eb8b..66668ea7b 100644 --- a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEvent.cs +++ b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEvent.cs @@ -1,12 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Webhooks.API.IntegrationEvents +namespace Webhooks.API.IntegrationEvents { - public class OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent + public class OrderStatusChangedToShippedIntegrationEvent { public int OrderId { get; private set; } public string OrderStatus { get; private set; } diff --git a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEventHandler.cs b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEventHandler.cs index 9a1dea33d..6e71bf5cd 100644 --- a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEventHandler.cs +++ b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/OrderStatusChangedToShippedIntegrationEventHandler.cs @@ -1,15 +1,13 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using System; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Threading.Tasks; +using DotNetCore.CAP; using Webhooks.API.Model; using Webhooks.API.Services; using Microsoft.Extensions.Logging; namespace Webhooks.API.IntegrationEvents { - public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler + public class OrderStatusChangedToShippedIntegrationEventHandler :ICapSubscribe { private readonly IWebhooksRetriever _retriever; private readonly IWebhooksSender _sender; @@ -21,6 +19,7 @@ namespace Webhooks.API.IntegrationEvents _logger = logger; } + //TODO [CapSubscribe(nameof(OrderStatusChangedToShippedIntegrationEvent)))] public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event) { var subscriptions = await _retriever.GetSubscriptionsOfType(WebhookType.OrderShipped); diff --git a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEvent.cs b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEvent.cs index 18821f9fc..f365fe785 100644 --- a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEvent.cs +++ b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEvent.cs @@ -1,12 +1,6 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Webhooks.API.IntegrationEvents +namespace Webhooks.API.IntegrationEvents { - public class ProductPriceChangedIntegrationEvent : IntegrationEvent + public class ProductPriceChangedIntegrationEvent { public int ProductId { get; private set; } diff --git a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEventHandler.cs b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEventHandler.cs index 10e1542c8..041101690 100644 --- a/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEventHandler.cs +++ b/src/Services/Webhooks/Webhooks.API/IntegrationEvents/ProductPriceChangedIntegrationEventHandler.cs @@ -1,16 +1,14 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; +using DotNetCore.CAP; namespace Webhooks.API.IntegrationEvents { - public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler + public class ProductPriceChangedIntegrationEventHandler : ICapSubscribe { - public async Task Handle(ProductPriceChangedIntegrationEvent @event) + //TODO [CapSubscribe(nameof(ProductPriceChangedIntegrationEvent))] + public Task Handle(ProductPriceChangedIntegrationEvent @event) { - int i = 0; + return Task.CompletedTask; } } } diff --git a/src/Services/Webhooks/Webhooks.API/Startup.cs b/src/Services/Webhooks/Webhooks.API/Startup.cs index 654b1c897..2c8360e76 100644 --- a/src/Services/Webhooks/Webhooks.API/Startup.cs +++ b/src/Services/Webhooks/Webhooks.API/Startup.cs @@ -1,11 +1,8 @@ using System; using System.Collections.Generic; -using System.Data.Common; using System.IdentityModel.Tokens.Jwt; -using System.Linq; using System.Reflection; -using System.Threading; -using System.Threading.Tasks; +using System.Threading; using Autofac; using Autofac.Extensions.DependencyInjection; using HealthChecks.UI.Client; @@ -14,22 +11,14 @@ using Microsoft.ApplicationInsights.ServiceFabric; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.ServiceBus; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; -using RabbitMQ.Client; using Swashbuckle.AspNetCore.Swagger; using Webhooks.API.Infrastructure; using Webhooks.API.IntegrationEvents; @@ -56,7 +45,7 @@ namespace Webhooks.API .AddSwagger(Configuration) .AddCustomHealthCheck(Configuration) .AddHttpClientServices(Configuration) - .AddIntegrationServices(Configuration) + .AddIntegrationEventHandler() .AddEventBus(Configuration) .AddCustomAuthentication(Configuration) .AddSingleton() @@ -106,8 +95,6 @@ namespace Webhooks.API c.OAuthClientId("webhooksswaggerui"); c.OAuthAppName("WebHooks Service Swagger UI"); }); - - ConfigureEventBus(app); } protected virtual void ConfigureAuth(IApplicationBuilder app) @@ -120,15 +107,7 @@ namespace Webhooks.API */ app.UseAuthentication(); - } - - protected virtual void ConfigureEventBus(IApplicationBuilder app) - { - var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe(); - eventBus.Subscribe(); - eventBus.Subscribe(); - } + } } static class CustomExtensionMethods @@ -226,47 +205,51 @@ namespace Webhooks.API return services; } + public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) { - var subscriptionClientName = configuration["SubscriptionClientName"]; - - if (configuration.GetValue("AzureServiceBusEnabled")) + services.AddCap(options => { - services.AddSingleton(sp => + options.UseInMemoryStorage(); + if (configuration.GetValue("AzureServiceBusEnabled")) { - 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 => + options.UseAzureServiceBus(configuration["EventBusConnection"]); + } + else { - var rabbitMQPersistentConnection = sp.GetRequiredService(); - var iLifetimeScope = sp.GetRequiredService(); - var logger = sp.GetRequiredService>(); - var eventBusSubcriptionsManager = sp.GetRequiredService(); - - var retryCount = 5; - if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"])) + options.UseRabbitMQ(conf => { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } + 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"]); + } - return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); - }); - } + if (!string.IsNullOrEmpty(configuration["SubscriptionClientName"])) + { + options.DefaultGroup = configuration["SubscriptionClientName"]; + } + }); + return services; + } + + public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services) + { + services.AddTransient(); //Subscribe for ProductPriceChangedIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToShippedIntegrationEvent + services.AddTransient(); //Subscribe for OrderStatusChangedToPaidIntegrationEvent - services.AddSingleton(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); return services; } @@ -298,54 +281,6 @@ namespace Webhooks.API return services; } - public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) - { - services.AddTransient>( - sp => (DbConnection c) => new IntegrationEventLogService(c)); - - if (configuration.GetValue("AzureServiceBusEnabled")) - { - services.AddSingleton(sp => - { - var logger = sp.GetRequiredService>(); - var serviceBusConnection = new ServiceBusConnectionStringBuilder(configuration["EventBusConnection"]); - return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); - }); - } - 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"])) - { - retryCount = int.Parse(configuration["EventBusRetryCount"]); - } - - return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount); - }); - } - - return services; - } - public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) { // prevent from mapping "sub" claim to nameidentifier. diff --git a/src/Services/Webhooks/Webhooks.API/Webhooks.API.csproj b/src/Services/Webhooks/Webhooks.API/Webhooks.API.csproj index 7dea3fc64..add57c27c 100644 --- a/src/Services/Webhooks/Webhooks.API/Webhooks.API.csproj +++ b/src/Services/Webhooks/Webhooks.API/Webhooks.API.csproj @@ -11,6 +11,10 @@ + + + + @@ -22,10 +26,6 @@ - - - -