Esempio n. 1
0
        public static IServiceCollection AddSqsPoller(
            this IServiceCollection services, SqsPollerConfig config, Type[] types)
        {
            services.AddSingleton <IConsumerResolver, ConsumerResolver>();

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

            services.AddTransient <IHostedService>(provider =>
            {
                AmazonSQSClient sqsClient;
                if (!string.IsNullOrEmpty(config.AccessKey) &&
                    !string.IsNullOrEmpty(config.SecretKey))
                {
                    sqsClient = new AmazonSQSClient(
                        config.AccessKey, config.SecretKey, CreateSqsConfig(config));
                }
                else
                {
                    sqsClient = new AmazonSQSClient(CreateSqsConfig(config));
                }

                return(new SqsPollerHostedService(
                           sqsClient,
                           config,
                           provider.GetRequiredService <IConsumerResolver>(),
                           provider.GetRequiredService <ILogger <SqsPollerHostedService> >()));
            });

            return(services);
        }
        public static IServiceCollection AddSqsPoller(
            this IServiceCollection services, SqsPollerConfig config, Assembly[] assembliesWithConsumers)
        {
            services.AddSingleton(config);
            services.AddSingleton <IConsumerResolver, ConsumerResolver>();
            services.AddSingleton(provider =>
            {
                if (!string.IsNullOrEmpty(config.AccessKey) &&
                    !string.IsNullOrEmpty(config.SecretKey))
                {
                    return(new AmazonSQSClient(
                               config.AccessKey, config.SecretKey, CreateSqsConfig(config)));
                }

                return(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);
        }
Esempio n. 3
0
        public static IServiceCollection AddSqsPoller(
            this IServiceCollection services, SqsPollerConfig config, Assembly[] assembliesWithConsumers)
        {
            var types = assembliesWithConsumers
                        .SelectMany(assembly => assembly.GetTypes())
                        .Where(type => type.IsClass && typeof(IConsumer).IsAssignableFrom(type))
                        .ToArray();

            return(AddSqsPoller(services, config, types));
        }
Esempio n. 4
0
 public SqsPollerHostedService(
     AmazonSQSClient amazonSqsClient,
     SqsPollerConfig config,
     IConsumerResolver consumerResolver,
     ILogger <SqsPollerHostedService> logger)
 {
     _amazonSqsClient  = amazonSqsClient;
     _config           = config;
     _consumerResolver = consumerResolver;
     _logger           = logger;
 }
        private static AmazonSQSConfig CreateSqsConfig(SqsPollerConfig config)
        {
            var amazonSqsConfig = new AmazonSQSConfig
            {
                ServiceURL = config.ServiceUrl
            };

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

            return(amazonSqsConfig);
        }