调试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  调试—>附加到进程,选择你的服务进程(如果找不到可以勾选 显示所有用户的进程),就可以了。
                        
方法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 运行就可以调试了。

调试完成后安装启动服务

原文链接:http://www.cnblogs.com/KSalomo/p/6519878.html?tdsourcetag=s_pctim_aiomsg

调试Windows Service的更多相关文章

  1. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  2. Win7中不能调试windows service

    多年前玩过一次windows service,觉得挺简单的. 这次工作要维护产品中的windows service,发现不是那么简单,vs附加调试器的窗体中无法找到windows service进程. ...

  3. windows Service 之调试过程(附加到进程里调试,而且启动时间不能超过30秒)

    最近第一次用C#写了一个windows service ,其实实现的内容比较简单.就是启动remoting 连接,但是调试相对初次写windws service 的我来说,比较烦.没有经验,而且没办法 ...

  4. C#创建Windows Service(Windows 服务)基础教程

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  5. C# Windows Service 基础

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  6. C#创建一个Windows Service

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  7. 使用Advanced Installer 13.1打包发布 Windows Service服务程序

    原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...

  8. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  9. Windows Service 开发,安装与调试

    Visual Studio.net 2010 Windows Service 开发,安装与调试 本示例完成一个每隔一分钟向C:\log.txt文件写入一条记录为例,讲述一个Windows Servic ...

随机推荐

  1. 蛋白质结构模型和功能预测:I-TASSER工具的使用

    I-TASSER是一款用于预测蛋白质结构和功能的工具,网站链接:https://zhanglab.ccmb.med.umich.edu/I-TASSER/ 具体描述如下: I-TASSER (Iter ...

  2. (大数取模)Big Number hdu1212

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. C#使用Ado.Net读写数据库

    1.使用DataReader方式读取资料 [csharp] view plain copy String connString = ConfigurationManager.ConnectionStr ...

  4. uwsgi多进程配合kafka-python消息无法发送

    在工作中,使用uwsgi部署项目,其中uwsgi设置为多进程,并且python中使用了kafka-python模块作为生产者不断产生数据,但上线不久后几乎所有的生产者消息都报:KafkaTimeout ...

  5. zabbix3.x添加H3C网络设备详解

    zabbix3.x添加H3C网络设备详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 前言: 欢迎加入:高级运维工程师之路 598432640 相信大家在看我的文章之前,也看过其 ...

  6. PHP composer-setup安装遇到的openssl extension is missing

    问题描述: 安装完成php-7.1.17后,安装composer出现以下错误 [root@localhost src]# curl -sS https://getcomposer.org/instal ...

  7. myeclipse设置,提高开发效率

    全局搜索,打开一个文件,却把另外一个给关掉. 解决办法: 在myeclipse中window-preferences-general-search找到第一行的一个选项reuse editors to ...

  8. 在eclipse中启动项目报java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space

    在我们启动项目的时候经常会出现内存溢出这个错误  设置一下内存就ok 错误信息 java.util.concurrent.ExecutionException: java.lang.OutOfMemo ...

  9. PHP5.5+Nginx1.9

    1. 安装Nginx:http://www.cnblogs.com/vurtne-lu/p/7010065.html 2. 编译安装 [root@zabbix opt]# wget http://cn ...

  10. golang数组声明

    格式 初始化数组 {}中的元素数不能大于[]中的数字,并且长度在初始化后不能改变,定义数组时需指定长度 ... var arrName [num]type = [num]type{value, val ...