调试Windows Service

使用一般的调试方法调试不了Windows Servers,所以参考了一些调试方法

我服务源码中使用了Timer,注意不能使用工具箱内的Timer,用System.Timers.Timer timer = new System.Timers.Timer()代替;

timer.Elapsed事件委托绑定的方法第三种方法调试的时候无法使用断点,不是timer不运行,目前不知道什么原因,所以调试时把代码放到OnStart()中调试。

默认服务名为Service1,改服务名的时候注意几个地方都要改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
using System.Data.SqlClient;
using System.Threading;
using System.Configuration;
using SAP.Middleware.Connector;
using TimerService.Helper;
using TimerService.JsonConvert;
using System.ServiceProcess;
using Newtonsoft.Json;
using System.Timers;

namespace TimerService
{
    public partial class TimerService : ServiceBase
    {

        System.Timers.Timer timer = new System.Timers.Timer();
        public TimerService()
        {
            InitializeComponent();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
            timer.Interval = 2000;
        }

        //protected override void OnStart(string[] args)
        public void OnStart()
        {
            timer.Enabled = true;
            timer.Start();
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
            timer.Stop();
        }

        private void timer_Tick(object sender, ElapsedEventArgs e)
        {
            //你的代码
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace TimerService
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            //ServiceBase[] ServicesToRun;
            //ServicesToRun = new ServiceBase[]
            //{
            //    new TimerService()
            //};
            //ServiceBase.Run(ServicesToRun);
            TimerService obj = new TimerService();
            obj.OnStart();
        }
    }
}

方法1:写日志
        是最传统的调试windows service方法,也是大家在调试service 比较管用的方式,但是,调试起来还是不太明朗。你要在你认为可能出现错误的地方全部添加写日志的方法。我上面的代码就采用了AddTextLine 函数实现的这种方法。

方法2:附加进程
        附加进程的方法可以像调试正常的widows程序一样,设置断点进行单步调试。但是,我必须在安装启动服务后,才可以进行附加此服务进程,可在附加的同时OnStart 函数已经执行完毕,所以对Onstart 无法调试。但是我可以通过设置启动服务延时来加载调试。
        步骤如下:
                     1,设置启动服务延时,
                           

复制代码
private System.Timers.Timer timerDelay;

        protected override void OnStart(string[] args)
        {
            try
            {
                
                ///delay start the SynData 30seconds
                timerDelay = new System.Timers.Timer(30000);   
                timerDelay.Elapsed += new System.Timers.ElapsedEventHandler(timerDelay_Elapsed);
                timerDelay.Start();
            }
            catch (Exception ex)
            {
                this.PrintExceptions(ex);
            }
        }

        void timerDelay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timerDelay.Enabled = false;
            timerDelay.Close();
            //你要加的代码
            //.To do..
        }
复制代码

                      注意:正常服务的启动时间为30秒左右,当服务启动时间超过30秒会报错!,所以不要在OnStart中做过多的操作,也可以用这种延时的方法启动服务,以防在启动服务时超时。
                     2、首先要对服务进行安装,然后启动服务。
                     3、打开vs2005  调试—>附加到进程,选择你的服务进程(如果找不到可以勾选 显示所有用户的进程),就可以了。
                        进程.jpg
方法3:
      我认为是这次调试对我帮助最大。
      在Main 函数中,注释掉原有自动生成的代码,注意红字部分是要根据自己的服务名字来手工添加的
             // ServiceBase[] ServicesToRun;

            // 同一进程中可以运行多个用户服务。若要将
            
// 另一个服务添加到此进程中,请更改下行以
            
// 创建另一个服务对象。例如,
            
//
            //   ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
            
//
            //ServicesToRun 
= new ServiceBase[] { new TeamWorldService() };

            //ServiceBase.Run(ServicesToRun);
            //******************************************
            TeamWorldService obj = new TeamWorldService();
            obj.OnStart();

            //******************************************
      然后把 protected override  void OnStart(string[] args) 改为 public void OnStart()。
      ,设置你的断点,按 F5 运行就可以调试了。

调试完成后安装启动服务

原文地址:https://www.cnblogs.com/KSalomo/p/6519878.html