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 { /// /// Extract EventService Configurations ... /// /// /// public static XEventServiceConfiguration GetXEventServiceConfiguration( this IConfiguration source ) { // var configSection = source .GetSection(ConfigurationNodeNames.EVENT_SERVICE_NODE_NAME); var result = configSection.Get(); // return result; } /// /// Register Event Service Configurations ... /// /// /// public static void AddXEventServiceConfiguration( this IServiceCollection services, IConfiguration configuration ) { // var config = configuration.GetXEventServiceConfiguration(); services.AddXEventServiceConfiguration(config); } /// /// Register Event Service Configurations ... /// /// /// 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); } /// /// Register Service ... /// /// /// /// public static void AddXEventService( this IServiceCollection source, IConfiguration configuration, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { // var config = configuration.GetXEventServiceConfiguration(); source.AddEventService( lifeTime: lifeTime, configuration: config ); } /// /// Register Service ... /// /// /// /// public static void AddXEventService( this IServiceCollection source, XEventServiceConfiguration configuration, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { // source.AddEventService( lifeTime: lifeTime, configuration: configuration ); } /// /// Event Consumer Service Registration ... /// /// /// public static void AddXEventServiceConsumer( this IServiceCollection source ) where TConsumer : XBaseEventConsumerBackgroundService { // var config = source.GetRegisteredService(); 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(); // Log($"Consumer Registered Successfully: {consumerType.Name} for Broker: {config.Broker.GetStringValue()} ..."); } // #region Private ... /// /// a LogTag for Service ... /// private static string XLogTag = XEventServiceConstants.XEventServiceDILogTag; /// /// print a log in Console ... /// /// private static void Log(string message) { Console.WriteLine($"{XLogTag} => {message}"); } /// /// Register Event Service ... /// /// /// /// 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 } }