2022-08-18 11:14:28 +04:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Basket.API;
|
|
|
|
|
|
|
|
|
|
public static class CustomExtensionMethods
|
|
|
|
|
{
|
2023-05-01 16:31:57 -07:00
|
|
|
|
public static ConfigurationManager AddKeyVault(this ConfigurationManager configuration)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.GetValue("UseVault", false))
|
|
|
|
|
{
|
|
|
|
|
var credential = new ClientSecretCredential(
|
|
|
|
|
configuration["Vault:TenantId"],
|
|
|
|
|
configuration["Vault:ClientId"],
|
|
|
|
|
configuration["Vault:ClientSecret"]);
|
|
|
|
|
|
|
|
|
|
configuration.AddAzureKeyVault(new Uri($"https://{configuration["Vault:Name"]}.vault.azure.net/"), credential);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddRedis(this IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
return services.AddSingleton(sp =>
|
|
|
|
|
{
|
|
|
|
|
var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value;
|
|
|
|
|
var configuration = ConfigurationOptions.Parse(settings.ConnectionString, true);
|
|
|
|
|
|
|
|
|
|
return ConnectionMultiplexer.Connect(configuration);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 11:14:28 +04:00
|
|
|
|
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var hcBuilder = services.AddHealthChecks();
|
|
|
|
|
|
|
|
|
|
hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy());
|
|
|
|
|
|
|
|
|
|
hcBuilder
|
|
|
|
|
.AddRedis(
|
|
|
|
|
configuration["ConnectionString"],
|
|
|
|
|
name: "redis-check",
|
|
|
|
|
tags: new string[] { "redis" });
|
|
|
|
|
|
|
|
|
|
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
|
|
|
|
{
|
|
|
|
|
hcBuilder
|
|
|
|
|
.AddAzureServiceBusTopic(
|
|
|
|
|
configuration["EventBusConnection"],
|
|
|
|
|
topicName: "eshop_event_bus",
|
|
|
|
|
name: "basket-servicebus-check",
|
|
|
|
|
tags: new string[] { "servicebus" });
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hcBuilder
|
|
|
|
|
.AddRabbitMQ(
|
|
|
|
|
$"amqp://{configuration["EventBusConnection"]}",
|
|
|
|
|
name: "basket-rabbitmqbus-check",
|
|
|
|
|
tags: new string[] { "rabbitmqbus" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
2023-05-01 16:31:57 -07:00
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
if (configuration.GetValue("AzureServiceBusEnabled", false))
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var serviceBusConnectionString = configuration["EventBusConnection"];
|
|
|
|
|
|
|
|
|
|
return new DefaultServiceBusPersisterConnection(serviceBusConnectionString);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
|
|
|
|
|
var eventBusSubscriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|
|
|
|
string subscriptionName = configuration["SubscriptionClientName"];
|
|
|
|
|
|
|
|
|
|
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
|
|
|
|
eventBusSubscriptionsManager, sp, subscriptionName);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
|
|
|
|
|
{
|
|
|
|
|
var subscriptionClientName = configuration["SubscriptionClientName"];
|
|
|
|
|
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
|
|
|
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
|
|
|
|
|
var eventBusSubscriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|
|
|
|
|
|
|
|
|
var retryCount = 5;
|
|
|
|
|
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
|
|
|
|
|
{
|
|
|
|
|
retryCount = int.Parse(configuration["EventBusRetryCount"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, sp, eventBusSubscriptionsManager, subscriptionClientName, retryCount);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|
|
|
|
|
|
|
|
|
services.AddTransient<ProductPriceChangedIntegrationEventHandler>();
|
|
|
|
|
services.AddTransient<OrderStartedIntegrationEventHandler>();
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
}
|