Processes to attach Windows Service to IDE and get control in onstart Event


There are around 5 ways to make mutex logic with respect to windows services and os interactions are concern. But best way to integrate IDE is using the .net built-in mutex logic using System.Diagnostics class.  By reading links in the glossary at end you can get to know what all are the other ways.

Step 1: 

Include/import System.Diagnostics Class to which Windows service you want. Then go to ONSTART event. 

Step 2: 

Copy and Paste below code on your ONSTART event very beginning. 

if (!System.Diagnostics.Debugger.IsAttached)
{
     System.Diagnostics.
Debugger.Launch();
     System.Diagnostics.
Debugger.Break();
}

Ex: Print Service

1.gif

Step 3: 

Run the Application in Debug Mode. 

Ex: Print Service 

2.gif

Then you will get the new exe for that project in its Debug folder.

Ex: Print Service 

3.gif

Check the system time and exe time by taking properties since we need to double check the change is happened.

Step 4: 

Install the exe from debug folder using this statement. 

"%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" /name="NameOfService" "Service EXE Full path\Debug\Service Name with extension Exe". 

Ex: Print Service 

"%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" /name="PrintService" "D:\PorrespondenceManager\PrintService\PrintService\bin\Debug\PrintService.exe" 

Step 5: 

Once it is installed it will be there in the Service. Before start the service if sometime we need to set local user permissions, if that is required you can do it by right clicking there properties of service and selecting the tab Logon. 

Ex: Print Service 

4.gif 

5.gif

Step 6: 

Start the service it will look like this. Attach the JIT-IN-TIME Debugger to service as well as .net IDE.  

6.gif

Select the application name and press yes. 

7.gif

Just press ok button and ignore it. 

8.gif

Once we attach IDE to Service Application look like this. And it breaks in the debugger.break() line as I show in the below image.    

9.gif

Immediate window values you will get like this 

10.gif

Watch window values you will get like this. 

11.gif

So that you can debug and get those values of your variables. 

Step 7: 

Once finish debug you need remove this code from ONSTART event and build the service once more time in release mode so it will work in normal way. 

if (!System.Diagnostics.Debugger.IsAttached)
{
     System.Diagnostics.
Debugger.Launch();
     System.Diagnostics.
Debugger.Break();

Step 8: 

Finished. 

Integrating Service to .net 2003 IDE through JIT-IN-TIME Debugger. 

Tools->Options. Then we will get below figure. 

12.gif

Check the common language runtime, Native,Script. And pres ok . so it will list in the JUST-IN-TIME debugger list. 

So it will list like this  

13.gif

You can see Microsoft CLR debugger 2003 is there in possible debugger list. 

Integrating Service to .net 2005 IDE through JIT-IN-TIME Debugger. 

Tools->Options. Then we will get below figure. 

14.gif

Check the common language runtime, Native, Script. And press OK. So it will list in the JUST-IN-TIME debugger list. 

So it will list like this  

15.gif

You can see Microsoft CLR debugger 2005 is there in possible debugger list. 

Glossary

  1. http://www.bluevisionsoftware.com/WebSite/TipsAndTricksDetails.aspx?Name=DebuggingServiceOnStart.
  2. http://blog.domaindotnet.com/2007/02/17/c-windows-service-debug-macro-auto-attach-while-developing/
  3. http://discuss.joelonsoftware.com/default.asp?dotnet.12.236451.7
  4. http://theimes.com/archive/2007/08/22/net-windows-service-runner.aspx
  5. http://69.10.233.10/KB/system/HowtoDebugaWindowsService.aspx
  6. http://www.codeproject.com/KB/vb/ServiceDebug.aspx
  7. http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger_members.aspx.
  8. http://vbnotebookfor.net/2007/07/24/how-to-make-debugging-net-windows-services-easier/
  9. http://www.pickabar.com/blog/archives/2004/01/three_little_ti.html
  10. http://www.daniweb.com/forums/thread61832.html
  11. http://arcanecode.wordpress.com/2007/05/24/windows-services-in-c-debugging-windows-services-part-4/
  12. http://www.computing.net/answers/dos/debugexe-for-dos-ver-401/2594.html
  13. http://stuff.mit.edu/afs/athena/software/fsharp_v1.1.12/FSharp-1.1.12.3/manual/debug.html
  14. http://support.microsoft.com/kb/824344
  15. http://msdn.microsoft.com/en-us/library/ssc8234s(VS.80,printer).aspx 

erver'>

Similar Articles