惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
P
Proofpoint News Feed
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
O
OpenAI News
G
Google Developers Blog
Martin Fowler
Martin Fowler
罗磊的独立博客
S
SegmentFault 最新的问题
T
Tor Project blog
量子位

博客园 - peak

windows 服务器时间同步失败处理方法 利用批处理自动创建schtasks系统任务 抓取网站编码信息及内容 在线调试利器Fiddler AutoResponse jquery跨域调用WCF Base-64 字符串中的无效字符 Ie7下鼠标滚轮失效 Android 笔记二(取得根目录权限) Android 笔记一 捕获asp.net ValidationSummary 控件消息。 - peak Sql Split 函数 Rss的浏览器之痛 兼容IE,Firefox 图片即时显示 asp.net 中插入flash - peak - 博客园 迷茫 windows service 之访问权限 比尔盖兹在某个大学毕业典礼上的演讲中,对毕业生提出十一项极为睿智的人生建议 MSDN上关于泛型的例子 泛型
windows Service 之调试过程
peak · 2008-05-30 · via 博客园 - peak

         最近第一次用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  ( " + 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,设置启动服务延时,

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 运行就可以调试了。

以上是自己这次调试widows service 后,得到的一些方法。以待今后继续积累。