views:

143

answers:

5

does anyone knows any good resources and articles that can explain how to create windows services using c#. thanks

+1  A: 

I liked this article: http://msdn.microsoft.com/en-us/magazine/cc301845.aspx

Aaron
+2  A: 

I'd suggest you Google for the different parts, different places on the web have different information focus (and level of up-to date info). You need to know about these topics:

  • Windows Service backgrounders: How services work in Windows. See how they are different in regards to user interface, system privileges (which account the service runs under, and what are its privileges), startup/shutdown, and user feedback.

  • One thing you may want to do in a service is use a Timer control to do periodic tasks. The "normal" Timer that you put on window forms does NOT work in a service because that control must be used with a UI window (which a service lacks). You have to use a System.Timer control. Google this for more info. I put this here because you will waste several hours wondering why your timer does not work, all of us that have written Services have gone through this!

  • Debugging - How do you debug a service? There are several options.

  • Deployment - Installation of a service is different and a little more involved than that of a Windows app. It's not a great mystery, there are simply a couple things more to worry about.

  • Interaction with the user - Services do not use a UI, so how are you going to enable your user to control the service (configure, start/stop, view log, etc...).

These are some of the topics that come to mind, if you read up on these things, you should be OK.

I'd also recommend testing your service deployments on a virtual machine so you can know for a fact that your setup works.

gmagana
+1  A: 

I would highly suggest a method like this for debugging purposes:

Service with console option

Sorry for the link quality, I can't seem to find the stack overflow related question. This has helped me a ton writing and debugging my services, and monitoring them while in a test environment.

thismat
+1  A: 

good learning:
Walkthrough: Creating a Windows Service Application in the Component Designer
Creating a Basic Windows Service in C#
Creating a Simple Windows Service in C#
How to create a setup project for a Windows Service application in Visual 2005/2008 C#

lsalamon
Good links, but just a caveat: Some of the tutorials out there have outdated information, or simply incomplete. The CodeProject one has the programmer using a batch file to install the service... That might be fine for some debugging scenarios, but it is by no means required with the VS2008 for a final deployment. So readers should be careful about what some walkthroughs say about services. Services are one of those things that have really been simplified in the later versions of VS; there is no more need to do anything outside of VS to deploy a windows service successfully.
gmagana
+1  A: 

Here are step-by-step instructions for creating a Windows service in C#.

Easiest language to create a windows service

After that, you can use these instructions to have the service install/uninstall itself from the command line as opposed to using the InstallUtil executable.

How to make a .NET Windows Service start right after the installation?

If you ever want to debug your service, put a call to System.Diagnostics.Debugger.Break() in the Main() function of your Windows service. When you start the service from the Windows Services MMC, the programmatic breakpoint will trigger a dialog box that allows you start a new debugging session (or use an existing instance of Visual Studio). The usual caveats apply - be sure to compile debug, make sure you have debugging permissions on the local machine, etc. If you want to skip the startup logic in Main(), you can put the programmatic breakpoint in your service's constructor or in the OnStart() callback.

Matt Davis