Exemplo n.º 1
0
        private static async Task MainAsync(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .AddJsonFile($"appsettings.json", false, true)
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddInMemoryCollection();
            var configuration = builder.Build();

            var webserviceOptions = new WebserviceOptions();

            configuration.Bind("WebserviceOptions", webserviceOptions);

            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterType <Consumer>().As <IConsumer>().SingleInstance();
            containerBuilder.RegisterType <LoggerFactory>().As <ILoggerFactory>().SingleInstance();
            containerBuilder.RegisterGeneric(typeof(Logger <>)).As(typeof(ILogger <>));
            containerBuilder.RegisterType <HttpClientWrapper>().As <IHttpClient>();
            containerBuilder.RegisterType <ConsoleWriter>().As <IConsoleWriter>().SingleInstance();

            // potentially register more sorters here, can rename the existing sorter to something a bit more specific later if required
            containerBuilder.RegisterType <FelineOwnerSorter>().As <IOwnerSorter>();
            containerBuilder.RegisterInstance(webserviceOptions);

            var container = containerBuilder.Build();

            container.Resolve <ILoggerFactory>().AddFile("FelineSorter-{Date}.log");

            // uncomment this to log to console
            //container.Resolve<ILoggerFactory>().AddConsole(Enum.Parse<LogLevel>(configuration["LogLevel"], true));

            var consumer = container.Resolve <IConsumer>();
            await consumer.Consume();
        }
Exemplo n.º 2
0
        public Consumer(ILogger <Consumer> logger, WebserviceOptions webserviceOptions, Func <Uri, IHttpClient> httpClientFactory, IOwnerSorter felineOwnerSorter, IConsoleWriter consoleWriter)
        {
            logger.ThrowIfArgumentNull(nameof(logger));
            webserviceOptions.ThrowIfArgumentNull(nameof(webserviceOptions));
            httpClientFactory.ThrowIfArgumentNull(nameof(httpClientFactory));
            felineOwnerSorter.ThrowIfArgumentNull(nameof(felineOwnerSorter));
            consoleWriter.ThrowIfArgumentNull(nameof(consoleWriter));

            _logger            = logger;
            _webserviceOptions = webserviceOptions;
            _httpClientFactory = httpClientFactory;
            _felineOwnerSorter = felineOwnerSorter;
            _consoleWriter     = consoleWriter;
        }