How to run a windows service project from visual studio.
I am building a windows serivce in visual studio 2008, I have to always run the service from control panel and then attach the debugger to running instance of the service. Its kind of annoying since I am cleaning a lot of code and need to restart my service many times during development.
I want to setup my project so as to be able to hit F5 and run the service and directly enter the debug mode. Some tips on how to achieve this would be great.
Thanks in advance!!!
These links can be very helpful when working with services.
This is a walk though on creating them http://msdn.microsoft.com/en-us/library/zt39148a.aspx
James Michael Hare has on his blog http://geekswithblogs.net/BlackRabbitCoder/ written about a really nice template/framework he has made, making it lot easier to develop (and debug) Windows Services: C# Toolbox: A Debuggable, Self-Installing Windows Service Template (1 of 2) http://geekswithblogs.net/BlackRabbitCoder/archive/2010/09/23/c-windows-services-1-of-2-creating-a-debuggable-windows.aspx
It provides you with all the basics you need to quickly get started. And best of all, it give you a really nice way to debug your service as if it was a regular console application. I could also mention that it provides out of the box functionality to install (and uninstall) your service. Part two of the post can be found at this link.
I've used this myself a couple of times, and can really recommend it.
Just call the OnStart() event from the service constructor
I did it in the following way
In your
Main()
routine check forDebugger.IsAttached
and if it's true start your app as if it's a console, if not, call intoServiceBase.Run()
.You can also do this: (See comments for explanation)
Copied from here.
This should allow you to run from within Visual Studio.
Another way would be to embed a programmatic breakpoint in your code by calling
System.Diagnostics.Debugger.Break()
. When you place this in, say, the OnStart() callback of your service and start your service from the Services console, the programmatic breakpoint will trigger a dialog box that allows you to attach to an existing instance of Visual Studio or to start a new instance. This is actually the mechanism I use to debug my service.You want to have your windows service as a shell, there should be little code in there so you don't have to test it.
You should have every thing you want your service to do in a class.
You can unit test you class and if it works then reference it to your service.
This way when you have you class doing every thing you want then when its applied to your service every thing should work. :)
Will an event log you can see what your service is doing while it is running, also a nice way to test :D try that.