Вопрос

I created a Windows service in C# (4.0) and am trying to install it using installutil tool in command line. However I get an exception. I managed to find out what part of my code is causing the exception - using some crappy logging but whatever - but now I want to understand why. So what I want to do is debugging the installation of my Windows Service.

I know how to debug the service itself, but here, I want to debug the content of my Installer.Install(IDictionary stateSaver) method in the service.

I tried to attach the debugger to the cmd.exe process but it obviously doesn't work. I was thinking also to attach the debugger to the installutil process but I have no clue how to do this.

I had a look to this post: How do you debug a windows service that is being installed? and several others but in this case, for some reason, this guy seem to have his service already in the services.msc which is not my case.

How can I achieve this?

Это было полезно?

Решение

You can put a Debugger.Break(); statement in the installer code, and it should launch the debugger for you.

Другие советы

If the above does not work, I have found this process works too. Basically, you compile in debug mode and install the service (I used installutil.exe through the command line). In code you pop-up a message box with the process ID. Startup a second instance of studio, attach it to that process and debug. The message box pauses it to allow setup. The process ID isn't important, its named InstallUtil.exe. I usually put a Debug.Break() in after the message box to guarantee it enters the code.

using System.Windows.Forms;
using System.Diagnostics;

...

#if DEBUG 
int processId = Process.GetCurrentProcess().Id;
string message = string.Format("Please attach the debugger (elevated on Vista or Win 7) to process [{0}].", processId);
MessageBox.Show(message, "Debug");
#endif

....

How to debug the installation of a custom windows service

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top