设为首页 收藏本站
查看: 1084|回复: 0

[经验分享] windows Service 之调试过程(附加到进程里调试,而且启动时间不能超过30秒)

[复制链接]

尚未签到

发表于 2017-12-8 12:41:37 | 显示全部楼层 |阅读模式
  最近第一次用C#写了一个windows service ,其实实现的内容比较简单。就是启动remoting 连接,但是调试相对初次写windws service 的我来说,比较烦。没有经验,而且没办法像调试其他windows 程序一样设置断点,无法看到运行过程。经过查看一些相关资料后,有了一点点调试的心得。特此留笔,以待今后使用。

相关源码:

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using Tribal.TeamWorld.API;
using Tribal.BaseClasses;
using Tribal.TeamWorld.Remoting;
using Tribal.TeamWorld.Implementation;
namespace Tribal.TeamWorld.Service
{
    partial class TeamWorldService : ServiceBase
    {
        private RemotingInterface _remotingInterface = null;
        private MainController _mainController = null;
        public TeamWorldService()
        {
            InitializeComponent();//
            
        }
        protected override  void OnStart(string[] args)
        {
            this.AddTextLine("Starting server DSC0000.gif ( " + DateTime.Now.ToString() + " )");
            try
            {
                this.AddTextLine("Initializing MainController ");
                this._mainController = MainController.GetInstance();
            }
            catch (Exception ex)
            {
                this.PrintExceptions(ex);
            }
            if (this._mainController != null)
            {
                this.AddTextLine("Connecting userinterface to LogController ");
            }
            try
            {
                this.AddTextLine("Starting MainController");
                this._mainController.Start();
            }
            catch (Exception exc)
            {
                this.PrintExceptions(exc);
            }
            try
            {
                this.AddTextLine("Starting .NET RemotingInterface");
                this._remotingInterface = new RemotingInterface();
                this._remotingInterface.StartServing();
            }
            catch (Exception exc)
            {
                this.PrintExceptions(exc);
            }
        }
        protected override void OnStop()
        {
            
            this._remotingInterface.StopServing();
            this._mainController.Stop();
            this.AddTextLine("End .NET RemotingInterface");
            
        }
        private void PrintExceptions(Exception exc)
        {
            Exception current = exc;
            while (current != null)
            {
                this.AddTextLine(current.Message);
                this.AddTextLine(current.StackTrace);
                current = current.InnerException;
            }
        }

        private void AddTextLine(string line)
        {
            try
            {
                FileStream fs = new FileStream(@"C:\TeamWorldServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine(line+ "\r\n");
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
               
            }
        }
    }
}  
方法1:写日志
        是最传统的调试windows service方法,也是大家在调试service 比较管用的方式,但是,调试起来还是不太明朗。你要在你认为可能出现错误的地方全部添加写日志的方法。我上面的代码就采用了AddTextLine 函数实现的这种方法。
方法2:附加进程
        附加进程的方法可以像调试正常的widows程序一样,设置断点进行单步调试。但是,我必须在安装启动服务后,才可以进行附加此服务进程,可在附加的同时OnStart 函数已经执行完毕,所以对Onstart 无法调试。但是我可以通过设置启动服务延时来加载调试。
        步骤如下:
                     1,设置启动服务延时,
                           


DSC0001.gif 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  调试—>附加到进程,选择你的服务进程(如果找不到可以勾选 显示所有用户的进程),就可以了。
                         DSC0002.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 运行就可以调试了。
以上是自己这次调试widows service 后,得到的一些方法。以待今后继续积累。
  http://www.cnblogs.com/peak-weng/archive/2008/05/30/1210538.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-422120-1-1.html 上篇帖子: 【Windows 10 应用开发】细说文本资源文件(resw) 下篇帖子: appium远程调用appium server
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表