Complete xEventService ...
This commit is contained in:
+163
-6
@@ -5,6 +5,8 @@ using xCommons.Extensions;
|
||||
using xEventService.Configuration;
|
||||
using xEventService.Constants;
|
||||
using xEventService.Extensions;
|
||||
using xEventService.Interfaces;
|
||||
using xEventService.Providers;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xEventService.DI
|
||||
@@ -16,7 +18,7 @@ namespace xEventService.DI
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static XEventServiceConfiguration GetXEventServiceConfigurations(
|
||||
public static XEventServiceConfiguration GetXEventServiceConfiguration(
|
||||
this IConfiguration source
|
||||
)
|
||||
{
|
||||
@@ -34,14 +36,14 @@ namespace xEventService.DI
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXEventServiceConfigurations(
|
||||
public static void AddXEventServiceConfiguration(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
var config = configuration.GetXEventServiceConfigurations();
|
||||
services.AddXEventServiceConfigurations(config);
|
||||
var config = configuration.GetXEventServiceConfiguration();
|
||||
services.AddXEventServiceConfiguration(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -49,14 +51,20 @@ namespace xEventService.DI
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXEventServiceConfigurations(
|
||||
public static void AddXEventServiceConfiguration(
|
||||
this IServiceCollection services,
|
||||
XEventServiceConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!configuration.IsValid())
|
||||
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 ...");
|
||||
@@ -68,6 +76,104 @@ namespace xEventService.DI
|
||||
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>
|
||||
@@ -83,6 +189,57 @@ namespace xEventService.DI
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user