Пример #1
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureServices((hostContext, services) =>
        {
            IConfiguration configuration = hostContext.Configuration;

            //getting config from appsettings
            WorkerOptions options = configuration.GetSection("MyWebsite").Get <WorkerOptions>();

            //by doing this, this is DI, passing this 'param' over. (so we have 2 param injected)
            services.AddSingleton(options);
            services.AddHostedService <Worker>();
        })
        .UseSerilog();
 /// <summary>
 /// init with 2 param as in program, 2 was sent via DI
 /// </summary>
 /// <param name="logger"></param>
 /// <param name="options"></param>
 public Worker(ILogger <Worker> logger, WorkerOptions options)
 {
     _logger  = logger;
     _options = options;
 }