pontoNETpt
A comunidade PontoNetPT está direccionada a todos os programadores que trabalhem com a plataforma .NET.
How to debug a windows service

The worst part on windows services development is that we cannot debug our application. So, its very hard to discover if one simple functionality is really working.

Yes, I already had this trouble and I fortunately have the solution for this.

Basically, we need to create some .bat files. They are:

  • Install service
  • Reinstall service
  • Uninstall service

We really don't need to create all three files, if you create just the install.bat its enough. The files' content is:

Install.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i ARMC.CWS.Import.WindowsService.exe
echo ---------------------------------------------------
echo Done.

Reinstall.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Uninstalling WindowsService...
echo ---------------------------------------------------
InstallUtil /u ARMC.CWS.Import.WindowsService.exe
echo ---------------------------------------------------
echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i ARMC.CWS.Import.WindowsService.exe
echo ---------------------------------------------------
echo Done.

Uninstall.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Uninstalling WindowsService...
echo ---------------------------------------------------
InstallUtil /u ARMC.CWS.Import.WindowsService.exe
echo ---------------------------------------------------
echo Done.

But why do we create this files? Because for test/debug our windows service we must to install it. There isn't another way to test instead installing it. But, with this files we can do this easily.

Ok, and now, how to debug the application?

Before you install it, you have to put some lines of code, on start method. This lines will give you some time to debug your application. See:

protected override void OnStart(string[] args) {

#if DEBUG
System.Threading.Thread.Sleep(10000);
#endif

// Do something - Put your breakpoint here

}

With these three lines of code you will be able to debug your application without any crazy procedures.

To do that, after installing the service, go to services window (inside administrative tools) and start your service.

image

Back to Visual Studio and go to Debug -> Attach to Process...

image

And choose your executable to debug. Note that this is only possible because we put a Thread.Sleep in the application. So, after you start the service, it will be sleeping about 10 seconds, sufficiently time to you attach the process to debug inside de Visual Studio. So you can debug every steps of your service.

 

Very nice...

It's very simple and easy to do that, but these tips and tricks are very useful.

God bless you!


Posted 10-5-2008 22:27 por Felipe Cavalcante Vilella de Oliveira
Filed under:

Add a Comment

(requerido)  
(opcional)
(requerido)  
Remember Me?
If you can't read this number refresh your screen
Enter the numbers above:  
Powered by Community Server (Commercial Edition), by Telligent Systems