|
|
@ -4,17 +4,10 @@ using Microsoft.ApplicationInsights.Extensibility; |
|
|
|
using Microsoft.ApplicationInsights.ServiceFabric; |
|
|
|
using Microsoft.AspNetCore.Builder; |
|
|
|
using Microsoft.AspNetCore.Hosting; |
|
|
|
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.Extensions.Configuration; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Payment.API.IntegrationEvents.EventHandling; |
|
|
|
using Payment.API.IntegrationEvents.Events; |
|
|
|
using RabbitMQ.Client; |
|
|
|
using System; |
|
|
|
using HealthChecks.UI.Client; |
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
|
|
@ -39,49 +32,40 @@ namespace Payment.API |
|
|
|
|
|
|
|
RegisterAppInsights(services); |
|
|
|
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
services.AddIntegrationEventHandler(); |
|
|
|
services.AddCap(options => |
|
|
|
{ |
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp => |
|
|
|
options.UseInMemoryStorage(); |
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
{ |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
|
|
|
|
|
|
|
var serviceBusConnectionString = Configuration["EventBusConnection"]; |
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); |
|
|
|
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
|
|
|
options.UseAzureServiceBus(Configuration["EventBusConnection"]); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
|
|
|
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"])) |
|
|
|
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"]; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
var container = new ContainerBuilder(); |
|
|
|
container.Populate(services); |
|
|
@ -110,9 +94,7 @@ namespace Payment.API |
|
|
|
app.UseHealthChecks("/liveness", new HealthCheckOptions |
|
|
|
{ |
|
|
|
Predicate = r => r.Name.Contains("self") |
|
|
|
}); |
|
|
|
|
|
|
|
ConfigureEventBus(app); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void RegisterAppInsights(IServiceCollection services) |
|
|
@ -132,56 +114,16 @@ namespace Payment.API |
|
|
|
new FabricTelemetryInitializer()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void RegisterEventBus(IServiceCollection services) |
|
|
|
{ |
|
|
|
var subscriptionClientName = Configuration["SubscriptionClientName"]; |
|
|
|
|
|
|
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
{ |
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp => |
|
|
|
{ |
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>(); |
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger, |
|
|
|
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp => |
|
|
|
{ |
|
|
|
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>(); |
|
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); |
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>(); |
|
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); |
|
|
|
|
|
|
|
var retryCount = 5; |
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"])) |
|
|
|
{ |
|
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]); |
|
|
|
} |
|
|
|
|
|
|
|
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); |
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
|
|
|
} |
|
|
|
|
|
|
|
private void ConfigureEventBus(IApplicationBuilder app) |
|
|
|
{ |
|
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>(); |
|
|
|
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent, OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static class CustomExtensionMethods |
|
|
|
{ |
|
|
|
public static IServiceCollection AddIntegrationEventHandler(this IServiceCollection services) |
|
|
|
{ |
|
|
|
services.AddTransient<OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); //Subscribe for OrderStatusChangedToStockConfirmedIntegrationEvent
|
|
|
|
return services; |
|
|
|
} |
|
|
|
|
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) |
|
|
|
{ |
|
|
|
var hcBuilder = services.AddHealthChecks(); |
|
|
|