245 lines
8.4 KiB
C#
245 lines
8.4 KiB
C#
using System;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xEventService.Configuration;
|
|
using xEventService.Constants;
|
|
using xEventService.Extensions;
|
|
using xEventService.Interfaces;
|
|
using xEventService.Providers;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xEventService.DI
|
|
{
|
|
public static partial class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Extract EventService Configurations ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XEventServiceConfiguration GetXEventServiceConfiguration(
|
|
this IConfiguration source
|
|
)
|
|
{
|
|
//
|
|
var configSection = source
|
|
.GetSection(ConfigurationNodeNames.EVENT_SERVICE_NODE_NAME);
|
|
var result = configSection.Get<XEventServiceConfiguration>();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Event Service Configurations ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXEventServiceConfiguration(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
var config = configuration.GetXEventServiceConfiguration();
|
|
services.AddXEventServiceConfiguration(config);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Event Service Configurations ...
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXEventServiceConfiguration(
|
|
this IServiceCollection services,
|
|
XEventServiceConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = configuration.IsValid() &&
|
|
(configuration.Broker == XEventBroker.XKafka ||
|
|
(configuration.Broker == XEventBroker.XZeroMQ &&
|
|
configuration.ZeroMQ.IsValid()) ||
|
|
(configuration.Broker == XEventBroker.XRabbitMQ &&
|
|
configuration.RabbitMQ.IsValid()));
|
|
if (!isValid)
|
|
{
|
|
//
|
|
Log("Registration Failed, due Invalid Configuration ...");
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
// Register Configuration as Singleton ...
|
|
services.AddSingleton(configuration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXEventService(
|
|
this IServiceCollection source,
|
|
IConfiguration configuration,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
var config = configuration.GetXEventServiceConfiguration();
|
|
source.AddEventService(
|
|
lifeTime: lifeTime,
|
|
configuration: config
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="lifeTime"></param>
|
|
public static void AddXEventService(
|
|
this IServiceCollection source,
|
|
XEventServiceConfiguration configuration,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
source.AddEventService(
|
|
lifeTime: lifeTime,
|
|
configuration: configuration
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event Consumer Service Registration ...
|
|
/// </summary>
|
|
/// <typeparam name="TConsumer"></typeparam>
|
|
/// <param name="source"></param>
|
|
public static void AddXEventServiceConsumer<TConsumer>(
|
|
this IServiceCollection source
|
|
) where TConsumer : XBaseEventConsumerBackgroundService
|
|
{
|
|
//
|
|
var config = source.GetRegisteredService<XEventServiceConfiguration>();
|
|
if (config.IsNullOrDefault())
|
|
{
|
|
//
|
|
Log("Consumer Registration Faild, due Invalid Configuration issue ...");
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
var consumerType = typeof(TConsumer);
|
|
switch (config.Broker)
|
|
{
|
|
//
|
|
case XEventBroker.XKafka:
|
|
//
|
|
if (!typeof(XKafkaConsumerServiceBase).IsAssignableFrom(consumerType))
|
|
{
|
|
//
|
|
Log($"Consumer Registration Failed. TConsumer must inherit from XKafkaConsumerServiceBase when Broker is {config.Broker.GetStringValue()} ...");
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
break;
|
|
|
|
//
|
|
case XEventBroker.XZeroMQ:
|
|
if (!typeof(XZeroMQConsumerServiceBase).IsAssignableFrom(consumerType))
|
|
{
|
|
Log($"Consumer Registration Failed. TConsumer must inherit from XZeroMQConsumerServiceBase when Broker is {config.Broker.GetStringValue()} ...");
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
break;
|
|
|
|
//
|
|
case XEventBroker.XRabbitMQ:
|
|
if (!typeof(XRabbitMQConsumerServiceBase).IsAssignableFrom(consumerType))
|
|
{
|
|
Log($"Consumer Registration Failed. TConsumer must inherit from XRabbitMQConsumerServiceBase when Broker is {config.Broker.GetStringValue()} ...");
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
break;
|
|
}
|
|
|
|
//
|
|
source.AddXBackgroundService<TConsumer>();
|
|
|
|
//
|
|
Log($"Consumer Registered Successfully: {consumerType.Name} for Broker: {config.Broker.GetStringValue()} ...");
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// a LogTag for Service ...
|
|
/// </summary>
|
|
private static string XLogTag = XEventServiceConstants.XEventServiceDILogTag;
|
|
|
|
/// <summary>
|
|
/// print a log in Console ...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
private static void Log(string message)
|
|
{
|
|
Console.WriteLine($"{XLogTag} => {message}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Event Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="lifeTime"></param>
|
|
private static void AddEventService(
|
|
this IServiceCollection source,
|
|
XEventServiceConfiguration configuration,
|
|
ServiceLifetime lifeTime = ServiceLifetime.Scoped
|
|
)
|
|
{
|
|
//
|
|
// Register Configuration ...
|
|
source.AddXEventServiceConfiguration(configuration);
|
|
|
|
//
|
|
// Register based on Broker ...
|
|
switch (configuration.Broker)
|
|
{
|
|
//
|
|
case XEventBroker.XKafka:
|
|
source.Add(
|
|
new ServiceDescriptor(typeof(IXKafkaProducerService), typeof(XKafkaProducerService), lifeTime)
|
|
);
|
|
break;
|
|
|
|
//
|
|
case XEventBroker.XZeroMQ:
|
|
source.Add(
|
|
new ServiceDescriptor(typeof(IXZeroMQProducerService), typeof(XZeroMQProducerService), lifeTime)
|
|
);
|
|
break;
|
|
|
|
//
|
|
case XEventBroker.XRabbitMQ:
|
|
source.Add(
|
|
new ServiceDescriptor(typeof(IXRabbitMQProducerService), typeof(XRabbitMQProducerService), lifeTime)
|
|
);
|
|
break;
|
|
}
|
|
|
|
//
|
|
source.Add(
|
|
new ServiceDescriptor(typeof(IXEventServiceProvider), typeof(XEventServiceProvider), lifeTime)
|
|
);
|
|
|
|
//
|
|
Log($"Service Registered Sucessfull, Provider: {configuration.Broker.GetStringValue()}");
|
|
}
|
|
#endregion
|
|
}
|
|
} |