debugging window service

牧云@^-^@ 提交于 2019-12-01 09:18:15

问题


I want to debug window service. What should i write in main() to enable debugging in window service. I am developing window service using C#.

#if(DEBUG)
      System.Diagnostics.Debugger.Break();
      this.OnStart(null);
      System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
 #else
      ServiceBase.Run(this);
 #endif

i wrote above code segment but on line (this


回答1:


I would do it like this:
In your service's OnStart method add the call to Debugger.Break() at the top:

protected override void OnStart(string[] args)
{
    #if DEBUG
        Debugger.Break();
    #endif

    // ... the actual code
}



回答2:


I personally use this method to debug a Windows service:

static void Main() {

    if (!Environment.UserInteractive) {
        // We are not in debug mode, startup as service

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyServer() };
        ServiceBase.Run(ServicesToRun);
    } else {
        // We are in debug mode, startup as application

        MyServer service = new MyServer();
        service.StartService();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

And create a new method in your MyServer class that will use the OnStart event:

public void StartService() {
    this.OnStart(new string[0]);
}



回答3:


Try this:

#if DEBUG
while (!System.Diagnostics.Debugger.IsAttached)
{
    Thread.Sleep(1000);
}
System.Diagnostics.Debugger.Break();
#endif

It waits until you attach a debugger, then breaks.




回答4:


This may be what you want to Do




回答5:


Check this Project in CodeBlex
Service Debugger Helper

I use this helper personally.



来源:https://stackoverflow.com/questions/6995565/debugging-window-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!