Exemple #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="onRun">Action to execute for run</param>
        /// <param name="onStop">Action to execute on stop</param>
        private IPBanServiceRunner(Func <CancellationToken, Task> onRun, Func <CancellationToken, Task> onStop)
        {
            Logger.Warn("Initializing service");
            Directory.SetCurrentDirectory(AppContext.BaseDirectory);
            OSUtility.AddAppDomainExceptionHandlers(AppDomain.CurrentDomain);
            var hostBuilder = new HostBuilder()
                              .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService <IPBanServiceRunner>(provider => this);
            });

            this.onRun  = onRun;
            this.onStop = onStop;
            if (Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService())
            {
                Logger.Warn("Running as a Windows service");
                hostBuilder.UseWindowsService();
            }
            else if (Microsoft.Extensions.Hosting.Systemd.SystemdHelpers.IsSystemdService())
            {
                Logger.Warn("Running as a systemd service");
                hostBuilder.UseSystemd();
            }
            else
            {
                // adding console lifetime wrecks things if actually running under a service
                Logger.Warn("Running as a console app");
                hostBuilder.UseConsoleLifetime();
            }
            hostBuilder.UseContentRoot(AppContext.BaseDirectory);
            host = hostBuilder.Build();
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="onRun">Action to execute for run</param>
        /// <param name="onStop">Action to execute on stop</param>
        private IPBanServiceRunner(Func <CancellationToken, Task> onRun, Func <CancellationToken, Task> onStop)
        {
            Logger.Warn("Initializing service");
            Directory.SetCurrentDirectory(AppContext.BaseDirectory);
            OSUtility.AddAppDomainExceptionHandlers(AppDomain.CurrentDomain);
            var hostBuilder = new HostBuilder()
                              .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService <IPBanServiceRunner>(provider => this);
            });

            this.onRun  = onRun;
            this.onStop = onStop;
            hostBuilder.UseContentRoot(AppContext.BaseDirectory);
            SetupHostService(hostBuilder);
            host = hostBuilder.Build();
        }