Solved

Debug Windows Service

Posted on 2013-06-28
2
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

738 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