示例#1
0
        ////See https://www.stevejgordon.co.uk/running-net-core-generic-host-applications-as-a-windows-service
        public static async Task Main(string[] args)
        {
            var isService = !(Debugger.IsAttached || args.Contains("--console"));

            if (isService)
            {
                var pathToExe         = Process.GetCurrentProcess().MainModule.FileName;
                var pathToContentRoot = Path.GetDirectoryName(pathToExe);
                Directory.SetCurrentDirectory(pathToContentRoot);
            }

            var builder = CreateWebHostBuilder(
                args.Where(arg => arg != "--console").ToArray());

            var host                      = builder.Build();
            var configuration             = host.Services.GetRequiredService <IConfiguration>();
            var logger                    = host.Services.GetRequiredService <ILogger <Program> >();
            var serviceControlsUrlMessage = $"The url which the service controls can be found are at: {configuration["Kestrel:Endpoints:Http:Url"]}";

            logger.LogInformation(serviceControlsUrlMessage);
            //Console.WriteLine(serviceControlsUrlMessage);

            if (isService)
            {
                var moneyWebHostService = new MoneyWebHostService(host, configuration, logger);
                ServiceBase.Run(new ServiceBase[] { moneyWebHostService });
            }
            else
            {
                var moneyService = new MoneyService(configuration, logger);
                await moneyService.StartAsync();

                host.Run();
            }
        }
 protected override void OnStarted()
 {
     base.OnStarted();
     Task.Run(() => _moneyService.StartAsync());
 }