in Uncategorized

Things you may need to know, when want to start working on Windows Services

As you may know, recently, I’ve done a project based on Windows Mobile for a hotel and because of their restaurants orders.

As one of their requests, they wanted me to use their previous ordering system in order to print bills. Their previous developer had been developed a parametric exe file that takes 4 parameters and then prints the bill out. So I needed to develop something on their servers in order to execute that old application. I decided to write a Windows Service that takes pocket pc requests via a wireless network connection and then print the bill out.

Working with Windows Service is not very often so you may not be able to find good articles for it but in this post I want to write about Windows Services.

Windows Service classes are very similar to normal classes except that you must implement them from the ServiceBase class. ServiceBase class is available in System.ServiceProcess namespace.

Keep in mind that you must import the System.ServiceProcess name space in your application because you will need this when want to work with Windows Services.

When you implemented your class from ServiceBase class, Visual Studio adds two methods to your class named “OnStart” and “OnStop”.

OnStart method:

This method allows you to write code you want to be executed when starting the service. You will need this method the most!

The OnStart method has an input parameter (A string array) which takes parameters that you may want to send to your service.

OnStop method:

This method will be executed when you’re stopping your service. Imagine that you’ve created a Thread in order to do something but you need to kill it when you stopped the service. In order to do such a thing, you have to make use of OnStop method.

Now what?

Adding an installer - Photo taken by myself. After writing your codes, it’s time to use your service. To make your service usable, you must add an installer to your application. Adding installer is easy when working with Microsoft Visual Studio; just right-click on your service designer and select “Add Installer” like the following picture.

When adding an installer, Visual Studio adds a class named “ProjectInstaller” and then adds two controls to your installer class named “ServiceInstaller” and “ServiceProcessInstaller”. By selecting each of those controls you can set some properties such as ServiceName, Description, DisplayName, etc.

Note: While setting the properties, don’t forget to verify the ServiceName property that must set to your service class.

You can also add your own codes to the ProjectInstaller class depend on your service.

How to install a Windows Service?

After building the service, you can access its files by going to the debug or release folder. You got two ways (Correct me if I’m wrong) to install a Windows Service.

· Programmatically.

· Manually.

Install or uninstall a Windows Service using Command Prompt - Photo taken by myself In this post, I want to install our service manually because it’s more common.

In order to install a Windows Service, you need to use Microsoft Visual Studio Command Prompt.

In the Command Prompt, we have an “installutil.exe” file that enables you to install and uninstall your Windows Services. Here is the syntax:

For install: installutils.exe [Your file path].
For uninstall: installutil.exe /u [Your file path].