Solved

Debug Windows Service

Posted on 2013-06-28
2
407 Views
Last Modified: 2013-06-29
Not that experienced in Windows Services Development

I see at this link that I can I can install the Service locally and then attach to Process in Visual Studio

Is there anything else I should know?

Does it have to be COM Visible or anything else?
0
Comment
Question by:lrbrister
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39286554
Hi,

I have recently done some research in windows service development and i have found the following tips very useful.

First of all, you probably have noticed that each time you need to stop start your service, and then attach to the process, and even install it if it is not already installed. I used to do all these steps manually until i found a solution.

In the Visual Studio open your Windows Service project.
Go to the Project Properties
Go to the Build Events tab (I think that the location of that tab depends on the studio version, so feel free to ask. You may need to look for a button in the complie tab)
There you may define PreBuild and PostBuild events.

So in the PreBuild you may write the following:
net stop <service name>
Exit /b 0

Open in new window

This will stop the service if it is running.

Then in the PostBuild screen add the following:
%WinDir%\Microsoft.NET\Framework\v2.0.50727\installutil /u "$(TargetPath)"
%WinDir%\Microsoft.NET\Framework\v2.0.50727\installutil "$(TargetPath)" 
net start <service name>
exit /b 0

Open in new window

This dictates the service to be uninstalled, then install a fresh copy and then start it.
If the service is not installed the first time you build it, it will fail, but just to another build and you are good to go

Another thing you may want to take notice, is the version of the framework. In my example i use .NET 2 installutil, but i think there is one for framework 4, so you might need to change the path.

This is a way for automating some staff you would have to do manually and it all came handy in my case...

So, now, attaching to the process of the service will help you debug a service that is up and running, but it won't help you debug the onstart method. So supposedly you do have code in the OnStart method, you can put the following in its first lines:

#if DEBUG then
     System.Diagnostics.Debugger.Break
#end if

Open in new window


The above code says to the compiler that this will run only if visual studio is set to debug mode (or you run from the debug folder). So, you wan't have to worry if you are in release mode. Then if you ARE in debug mode it will pop up a window asking you if you wish to debug the program and with what tool you want to do this. Just choose a new visual studio instance and happy debugging....

I hope this helps,
Giannis
0
 

Author Closing Comment

by:lrbrister
ID: 39286607
Perfect
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question