Solved

Debug Windows Service

Posted on 2013-06-28
2
403 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now