public Consumer( ConfigurationConsumer configurationConsumer, IServiceProvider serviceProvider) : this() { _configurationConsumers.Add(configurationConsumer); _serviceProvider = serviceProvider; }
private IConsumer <Ignore, string> GenerateTask(ConfigurationConsumer configConsumer) { var conf = new ConsumerConfig { GroupId = configConsumer.GroupId, BootstrapServers = configConsumer.Server, AutoOffsetReset = AutoOffsetReset.Earliest, }; var consumer = new ConsumerBuilder <Ignore, string>(conf).Build(); consumer.Subscribe(configConsumer.Topic); return(consumer); }
public static IServiceCollection AddConsumerKafa( this IServiceCollection services, string server) { if (string.IsNullOrEmpty(server)) { throw new ArgumentNullException(nameof(server)); } var types = GetTypesConsumer(); if (types == null || !types.Any()) { throw new NotConfiguredException("No topic has been configured to consume"); } var configurationsConsumers = new List <ConfigurationConsumer>(); foreach (var type in types) { var attribute = type.GetCustomAttribute <ConsumerKafkaAttribute>(); var configurationConsumer = new ConfigurationConsumer(server) { GroupId = attribute.GroupId, TypeConsumer = type, Topic = attribute.Topic }; services.AddSingleton(configurationConsumer.TypeConsumer); configurationsConsumers.Add(configurationConsumer); } services.AddSingleton(sp => { return(new Consumer(configurationsConsumers, sp)); }); services.AddHostedService <ConsumerBackgroundService>(); return(services); }
private IConsumerKafkaTopic GetService(ConfigurationConsumer configurationConsumers) => _serviceProvider.GetService(configurationConsumers.TypeConsumer) as IConsumerKafkaTopic;