Search code examples
c#windowswindows-services

How to debug a Windows service which stops immediately after it starts


I currently found an issue that one of my projects, which is a windows service, stops immediately after it starts. It was not like this before.

Worse still, I am unable to attach the service to Visual Studio since it stopped immediately after it started.

My system is Windows server 2012 R2 and Visual Studio is 2013.

Is there any way for me to debug the service?


Solution

  • I have just found an answer in MSDN.

    https://msdn.microsoft.com/en-us/library/cktt23yw.aspx

    Add the following code:

     System.Diagnostics.Debugger.Launch();
    

    At the very beginning of OnStart method. Then run net start serviceName (administrative credential is needed)