예제 #1
0
 /// <summary>
 /// Start generic ipban service with callbacks. The service implementation should have already been created before this method is called.
 /// </summary>
 /// <param name="args">Args</param>
 /// <param name="start">Start callback, start your implementation running here</param>
 /// <param name="stop">Stop callback, stop your implementation running here</param>
 /// <param name="requireAdministrator">Whether administrator access is required</param>
 /// <returns>Task</returns>
 public static async Task MainService(string[] args, Func <string[], Task> start, Action stop, bool requireAdministrator = true)
 {
     try
     {
         using IPBanServiceRunner runner = new IPBanServiceRunner(args, start, stop);
         await runner.RunAsync(requireAdministrator);
     }
     catch (Exception ex)
     {
         ExtensionMethods.FileWriteAllTextWithRetry(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "startup_fail.txt"), ex.ToString());
         Logger.Fatal("Fatal error starting service", ex);
     }
 }
예제 #2
0
        /// <summary>
        /// Run service helper method
        /// </summary>
        /// <param name="args">Args</param>
        /// <param name="onStart">Start</param>
        /// <param name="onStop">Stop</param>
        /// <param name="cancelToken">Cancel token</param>
        /// <returns>Task</returns>
#pragma warning disable IDE0060 // Remove unused parameter
        public static async Task MainService(string[] args, Func <Task> onStart, Func <Task> onStop = null, CancellationToken cancelToken = default)
#pragma warning restore IDE0060 // Remove unused parameter
        {
            try
            {
                using IPBanServiceRunner runner = new IPBanServiceRunner(onStart, onStop);
                await runner.RunAsync(cancelToken);
            }
            catch (Exception ex)
            {
                ExtensionMethods.FileWriteAllTextWithRetry(System.IO.Path.Combine(AppContext.BaseDirectory, "service_error.txt"), ex.ToString());
                Logger.Fatal("Fatal error running service", ex);
            }
        }
예제 #3
0
 public IPBanWindowsServiceRunner(IPBanServiceRunner runner, string[] args)
 {
     runner.ThrowIfNull();
     try
     {
         Logger.Warn("Running as a Windows service");
         this.runner = runner;
         CanShutdown = false;
         Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
     }
 }
예제 #4
0
        /// <summary>
        /// Run service helper method
        /// </summary>
        /// <param name="args">Args</param>
        /// <param name="onRun">Run</param>
        /// <param name="onStop">Stop</param>
        /// <returns>Task</returns>
#pragma warning disable IDE0060 // Remove unused parameter
        public static async Task <int> MainService(string[] args, Func <CancellationToken, Task> onRun, Func <CancellationToken, Task> onStop = null)
#pragma warning restore IDE0060 // Remove unused parameter
        {
            try
            {
                using IPBanServiceRunner runner = new IPBanServiceRunner(onRun, onStop);
                await runner.RunAsync();
            }
            catch (OperationCanceledException)
            {
                // don't care
            }
            catch (Exception ex)
            {
                ExtensionMethods.FileWriteAllTextWithRetry(System.IO.Path.Combine(AppContext.BaseDirectory, "service_error.txt"), ex.ToString());
                Logger.Fatal("Fatal error running service", ex);
                return(ex.HResult);
            }
            return(0);
        }
예제 #5
0
 public IPBanWindowsServiceRunner(IPBanServiceRunner runner, string[] args)
 {
     runner.ThrowIfNull();
     try
     {
         Logger.Warn("Running as a Windows service");
         this.runner = runner;
         CanShutdown = false;
         CanStop     = CanHandleSessionChangeEvent = CanHandlePowerEvent = true;
         var acceptedCommandsField = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
         if (acceptedCommandsField != null)
         {
             int acceptedCommands = (int)acceptedCommandsField.GetValue(this);
             acceptedCommands |= 0x00000100; // SERVICE_ACCEPT_PRESHUTDOWN;
             acceptedCommandsField.SetValue(this, acceptedCommands);
         }
         Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
     }
 }