Advertisement
Guest User

Untitled

a guest
Jan 27th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2.  
  3. // ==============================================================================================
  4. // For testing windows service with .NET 5
  5. // Source: https://stackoverflow.com/questions/7764088/net-console-application-as-windows-service
  6. // ==============================================================================================
  7.  
  8. using System.ServiceProcess;
  9. using System.IO;
  10.  
  11. public static class Program
  12. {
  13.         #region Nested classes to support running as service
  14.         public const string ServiceName = "MyService";
  15.  
  16.         public class Service : ServiceBase
  17.         {
  18.                 public Service()
  19.                 {
  20.                         File.AppendAllLines("log.txt", new string[] { "Service init" });
  21.                         ServiceName = Program.ServiceName;
  22.                 }
  23.  
  24.                 protected override void OnStart(string[] args)
  25.                 {
  26.                         File.AppendAllLines("log.txt", new string[] { "Service start" });
  27.                         Program.Start(args);
  28.                         base.OnStart(args);
  29.                 }
  30.  
  31.                 protected override void OnStop()
  32.                 {
  33.                         File.AppendAllLines("log.txt", new string[] { "Service stop" });
  34.                         Program.Stop();
  35.                         base.OnStop();
  36.                 }
  37.         }
  38.         #endregion
  39.  
  40.         static void Main(string[] args)
  41.         {
  42.                 File.AppendAllLines("log.txt", new string[] { "App init" });
  43.                 if (!Environment.UserInteractive)
  44.                 {
  45.                         File.AppendAllLines("log.txt", new string[] { "Running as service" });
  46.                         // running as service
  47.                         using (var service = new Service())
  48.                                 ServiceBase.Run(service);
  49.                 }
  50.                 else
  51.                 {
  52.                         File.AppendAllLines("log.txt", new string[] { "Running as app" });
  53.                         // running as console app
  54.                         Start(args);
  55.  
  56.                         Console.WriteLine("Press any key to stop...");
  57.                         Console.ReadKey(true);
  58.  
  59.                         Stop();
  60.                 }
  61.         }
  62.  
  63.         private static void Start(string[] args)
  64.         {
  65.                 // onstart code here
  66.         }
  67.  
  68.         private static void Stop()
  69.         {
  70.                 // onstop code here
  71.         }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement