|
@ -52,8 +52,8 @@ namespace Webhooks.API |
|
|
.AddCustomHealthCheck(Configuration) |
|
|
.AddCustomHealthCheck(Configuration) |
|
|
.AddDevspaces() |
|
|
.AddDevspaces() |
|
|
.AddHttpClientServices(Configuration) |
|
|
.AddHttpClientServices(Configuration) |
|
|
.AddIntegrationServices(Configuration) |
|
|
|
|
|
.AddEventBus(Configuration) |
|
|
.AddEventBus(Configuration) |
|
|
|
|
|
.AddIntegrationEventHandlers() |
|
|
.AddCustomAuthentication(Configuration) |
|
|
.AddCustomAuthentication(Configuration) |
|
|
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>() |
|
|
.AddSingleton<IHttpContextAccessor, HttpContextAccessor>() |
|
|
.AddTransient<IIdentityService, IdentityService>() |
|
|
.AddTransient<IIdentityService, IdentityService>() |
|
@ -121,18 +121,28 @@ namespace Webhooks.API |
|
|
app.UseAuthentication(); |
|
|
app.UseAuthentication(); |
|
|
app.UseAuthorization(); |
|
|
app.UseAuthorization(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void ConfigureEventBus(IApplicationBuilder app) |
|
|
protected virtual void ConfigureEventBus(IApplicationBuilder app) |
|
|
{ |
|
|
{ |
|
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>(); |
|
|
|
|
|
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>(); |
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>(); |
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>(); |
|
|
|
|
|
|
|
|
app.UseEventBus(eventBus => |
|
|
|
|
|
{ |
|
|
|
|
|
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>(); |
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>(); |
|
|
|
|
|
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>(); |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static class CustomExtensionMethods |
|
|
static class CustomExtensionMethods |
|
|
{ |
|
|
{ |
|
|
|
|
|
public static IServiceCollection AddIntegrationEventHandlers(this IServiceCollection services) |
|
|
|
|
|
{ |
|
|
|
|
|
return services |
|
|
|
|
|
.AddTransient<ProductPriceChangedIntegrationEventHandler>() |
|
|
|
|
|
.AddTransient<OrderStatusChangedToShippedIntegrationEventHandler>() |
|
|
|
|
|
.AddTransient<OrderStatusChangedToPaidIntegrationEventHandler>(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public static IServiceCollection AddAppInsight(this IServiceCollection services, IConfiguration configuration) |
|
|
public static IServiceCollection AddAppInsight(this IServiceCollection services, IConfiguration configuration) |
|
|
{ |
|
|
{ |
|
|
services.AddApplicationInsightsTelemetry(configuration); |
|
|
services.AddApplicationInsightsTelemetry(configuration); |
|
@ -217,49 +227,6 @@ namespace Webhooks.API |
|
|
|
|
|
|
|
|
return services; |
|
|
return services; |
|
|
} |
|
|
} |
|
|
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration) |
|
|
|
|
|
{ |
|
|
|
|
|
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.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>(); |
|
|
|
|
|
services.AddTransient<ProductPriceChangedIntegrationEventHandler>(); |
|
|
|
|
|
services.AddTransient<OrderStatusChangedToShippedIntegrationEventHandler>(); |
|
|
|
|
|
services.AddTransient<OrderStatusChangedToPaidIntegrationEventHandler>(); |
|
|
|
|
|
return services; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) |
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration) |
|
|
{ |
|
|
{ |
|
@ -289,55 +256,6 @@ namespace Webhooks.API |
|
|
return services; |
|
|
return services; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) |
|
|
|
|
|
{ |
|
|
|
|
|
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( |
|
|
|
|
|
sp => (DbConnection c) => new IntegrationEventLogService(c)); |
|
|
|
|
|
|
|
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled")) |
|
|
|
|
|
{ |
|
|
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp => |
|
|
|
|
|
{ |
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>(); |
|
|
|
|
|
var serviceBusConnection = new ServiceBusConnectionStringBuilder(configuration["EventBusConnection"]); |
|
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp => |
|
|
|
|
|
{ |
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>(); |
|
|
|
|
|
|
|
|
|
|
|
var factory = new ConnectionFactory() |
|
|
|
|
|
{ |
|
|
|
|
|
HostName = configuration["EventBusConnection"], |
|
|
|
|
|
DispatchConsumersAsync = true |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) |
|
|
{ |
|
|
{ |
|
|
// prevent from mapping "sub" claim to nameidentifier.
|
|
|
// prevent from mapping "sub" claim to nameidentifier.
|
|
|