2

How can I debug windows services? I cannot lunch the service from the Visual Studio - first I need to install the service using InstallUtil.exe and then I can start the service from the Windows Services Administrative tool.

Currently I am Attaching my Solution to a running process from Visual Studio Debug tab after starting the service from the Windows Services Administrative tool) .

Is there a more convenient way to do so, or maybe there is a way to configure the service to be self installed (as I run the project from the IDE)?

3
  • Ok I will try, so if I'll add the InstallUtil.exe command in my post build, I'll be enable to debug the service as I run it from Visual Studio? :) Commented Nov 24, 2015 at 7:53
  • I've added "net start MyAwesomeService" to my post build event, I've ensured that the service is stopped before I am running. Still getting this message of "Windows Start Failure" which tells me that I need to install the service first, which I did... Commented Nov 24, 2015 at 8:18
  • I've tried a different approach: first I've started the service, then I launched my project, but I've got another exception: The process cannot access the file 'bin\Debug\MyAwesomeService.exe' because it is being used by another process. Probably cause this exe file is used my the OS? Commented Nov 24, 2015 at 8:19

3 Answers 3

2

A bit of a cheat perhaps, but I used a pragma to force a debug.

In your Main of program.cs, try adding something like the following:

#if DEBUG
            YourService service = new YourService();
            service.OnDebug();

            // This prevents timeouts while debugging  
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                        new YourService()
            };
            ServiceBase.Run(ServicesToRun);
#endif

Then, in your service.cs file, add the following:

public void OnDebug()
{
     OnStart(null);
}

That just spoofs the calling of the start. When not in debug mode it executes as per normal.

Sign up to request clarification or add additional context in comments.

1 Comment

As mentioned it is a bit of a cheat, but that appears to be exactly what the question was asking for, not so? In any case, the service as in the example above is actually a service, and it runs fine as a service, the "debug mode" is just executed for debug purposes
1

You can start your service as console application and call your functions (start/stop/etc) from main function.

Or you can use unit tests.

2 Comments

run service as console app, is efficient way to debug code, "ecosystem" difference is another level of issues
Exactly, even with limitation this method is still convenient. Moreover, testing and bug fixing are different. If this approach can provide convenient, and as result efficient, debugging than why not? Even if will cover not 100% but 95% of issues.
0

In order to debug Windows Service you will need to attach Windows Service process to Visual Studio. See my screenshot below:

enter image description here

Let me know please, if you have any questions.

1 Comment

Yep, that's what I've described in my question. I am looking for a different, more convenient way (if exists) for windows services debugging...:) For Instance if I am attaching my project to a running process I am missing the service init\start which I really want to catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.