如何调试Windows服务而不安装它 [英] how to debug a windows services without installing it

查看:64
本文介绍了如何调试Windows服务而不安装它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调试Windows服务而不安装它

how to debug a windows services without installing it

推荐答案

我想你自己编写了这个服务?



一个调试可能性:



1)为您的服务类添加一个方法,如:



I suppose you''ve written the service on your own?

One debug possibility:

1) Add a method to your service class like:

public partial class YourService : ServiceBase
{
    public void DebugStart()
    {
        this.OnStart(null);
    }

}





主要应该是这样的,例如





And the main should look like this, e.g.

static void Main()
{
    if (System.Environment.UserInteractive)
    {
#if DEBUG
        YourService s = new YourService();
        s.DebugStart();
        
        bool run = true;
        
        do
        {
        // dirty!
           Thread.Sleep(1000);
        } while(run);
        
        
        s.Stop();
        s.Dispose();
#endif
    }
    else
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new YourService() 
        };
        ServiceBase.Run(ServicesToRun);

    }
}





您可以打开模态窗口,而不是脏的永久循环。要停止服务并调试服务关闭,请在forever循环中设置断点并设置run = false。



Instead of the dirty forever loop you could open a modal window. To stop the service and debug the service shutdown, set a breakpoint in the forever loop and set run = false.


这篇关于如何调试Windows服务而不安装它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆