debug 调试Windows service服务调试

Service1.cs

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WindowsService1
{
  public partial class Service1 : ServiceBase
  {
    public Service1()
    {
      InitializeComponent();
    }

    public void OnDebug()
    {
      string[] str = { "0" };
      OnStart(str);
    }

    protected override void OnStart(string[] args)
    {
      File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
    }

    protected override void OnStop()
    {
      File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
    }

  }
}
复制代码

Program.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WindowsService1
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
#if DEBUG  
      Console.WriteLine("This is debug mode");
      Service1 myService = new Service1();
      myService.OnDebug();
#else
      ServiceBase[] ServicesToRun;
      ServicesToRun = new ServiceBase[]
      {
                new Service1()
      };
      ServiceBase.Run(ServicesToRun);
#endif

    }
  }
}
复制代码
原文地址:https://www.cnblogs.com/BoKeYuan259/p/15508476.html