Solved

Debug Windows Service

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

13 Experts available now in Live!

Get 1:1 Help Now