2

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?

Community
  • 1
  • 1
Guillaume
  • 1,782
  • 1
  • 25
  • 42
  • What exactly is the exception you're getting while installing the Windows Service ? – abhilash Feb 29 '12 at 11:46
  • @ABKolan I get `TypeInitializerException` on a static constructor of a class I'm using (and I created) caused by a `NullReferenceException` on an object in this constructor. This object is related to some custom Configuration class I created. The weird thing is that this piece of code is also used by another service and everything works fine in that one. This is the reason why I want to debug the installation process. – Guillaume Feb 29 '12 at 11:50
  • Use the solution in http://stackoverflow.com/a/9498566/64497 – abhilash Feb 29 '12 at 11:52

2 Answers2

12

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

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • @sotn you could ask a new question to get help. Remember to include any errors you get. – Klaus Byskov Pedersen Feb 02 '17 at 09:20
  • 3
    @KlausByskovPedersen I solved that by using Debugger.Launch().. The reason for that error was if there is no debugger available, Debugger.Break will crash your application whereas Debugger.Launch will launch if non available.. – yakya Feb 06 '17 at 20:58
1

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

mminneman
  • 426
  • 3
  • 7
  • This helped me. I used WPF with `#if DEBUG` `System.Windows.MessageBox.Show("Attach debugger now");` `#endif`. It's pretty easy to find the InstallUtil process in the VS Attach to Process dialog. That way you don't have to add any additional `using` statements. – Joe Skeen Apr 11 '17 at 22:38