Exemple #1
0
 public SqsPollerHostedService(
     AmazonSQSClient amazonSqsClient,
     SqsPoolerConfig config,
     IConsumerResolver consumerResolver,
     ILogger <SqsPollerHostedService> logger)
 {
     _amazonSqsClient  = amazonSqsClient;
     _config           = config;
     _consumerResolver = consumerResolver;
     _logger           = logger;
 }
Exemple #2
0
        private static AmazonSQSConfig CreateSqsConfig(SqsPoolerConfig config)
        {
            var amazonSqsConfig = new AmazonSQSConfig
            {
                ServiceURL = config.ServiceUrl
            };

            if (!string.IsNullOrEmpty(config.Region))
            {
                amazonSqsConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(config.Region);
            }

            return(amazonSqsConfig);
        }
Exemple #3
0
        public static IServiceCollection AddSqsPoller(
            this IServiceCollection services, SqsPoolerConfig config, Assembly[] assembliesWithConsumers)
        {
            services.AddSingleton(config);
            services.AddSingleton <IConsumerResolver, ConsumerResolver>();
            services.AddSingleton(provider => new AmazonSQSClient(CreateSqsConfig(config)));

            services.AddTransient <IHostedService, SqsPollerHostedService>();

            var types = assembliesWithConsumers.SelectMany(assembly => assembly.GetTypes())
                        .Where(type => type.IsClass && typeof(IConsumer).IsAssignableFrom(type))
                        .ToArray();

            foreach (var type in types)
            {
                services.AddSingleton(typeof(IConsumer), type);
            }

            return(services);
        }