0

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

Karim
  • 6,113
  • 18
  • 58
  • 83

5 Answers5

2

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.

Gabriel Magana
  • 4,338
  • 24
  • 23
2

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.

Community
  • 1
  • 1
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
1

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

Aaron
  • 9,123
  • 5
  • 40
  • 38
1

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
  • 2,096
  • 17
  • 24
0

Look at Topshelf.

Topshelf is a framework for hosting services written using the .NET framework. The creation of services is simplified, allowing developers to create a simple console application that can be installed as a service using Topshelf. The reason for this is simple: It is far easier to debug a console application than a service. And once the application is tested and ready for production, Topshelf makes it easy to install the application as a service.

Andrey K.
  • 665
  • 8
  • 29