IF you have ever wondered how to create your own Windows Service then you’ve come to the right place! Creating a Windows application is rather straight forward.  You create the Solution, you add the Windows App project, add your Business logic and it is practically there.  With a Windows Service, there are some additional steps that need to be setup that might be somewhat new to developers out there.

In this article I will explain how to create a Windows Solution and the steps needed to deploy the Windows Service…

Creating the Solution and Projects

A)  Start by creating a Windows Service project from the “New Project dialog”.  I have called mine: MyService

B) Next: Rename the default class name that Visual Studio generated for you to something more appropriate. I have renamed mine to: MyService.CS

C) The next tricky bit is to add a New file to the Project called a Installer Class. I have called mine: MyServiceInstaller.CS

This installer is responsible for setting up the Service when it is deployed. Included in this class are the Startup properties, the Name and the Description that appears within the Computer Management -> Services Window.

D) After you have added the Installer Class go to your Toolbox on the Left Hand side. Try and locate the two controls called:

  • ServiceInstaller
  • ServiceProcessInstaller

If you cannot see these in your list of controls don’t worry.

  • Right click to get the context menu popup.
  • Select to Choose from a List.
  • In the Choose Toolbox Item dialog you can find these two components under the System.ServiceProcess namespace.

Once added, you should see something similar to the image below:

E) Double click on both of the components.  This will add one of each component into your Installer Class designer file.  Now it’s time to setup the properties of your Service.

Click on the ServiceInstaller component and the properties should appear as the image below.

  • Set the Description, DisplayName and ServiceName as you want it to appear once the service is installed.
  • Set the StartType to be Automatic (or manual as you please).
  • Finally set the Modifiers to Public.  I have always used Public since I know it works well. But Private shouldn’t stop you from installing the service.

Once that’s done let’s setup the ServiceProcessInstaller Properties:

  • For the Account – select NetworkService.  Or if you prefer to use a Windows Account for executing this service, select User in the dropdown.
  • Set the Modifier to Public (or Private if you prefer to).
  • And finally ensure the Parent is set to the Installer Class name you provided.

That’s it!! Once that is done the Service is now ready to go!

Adding the Installation Setup Project

F) Add a new Setup Project into your VS Solution. I have called mine: MyServiceSetup

G) Next, you need to specify what you want the Setup Project to install in your deployment. Right -Click the Setup Project and select Add -> Project Output

If you have multiple projects in the solution, select your Service Project from the list. Also ensure the Primary Output of your selected Project is chosen.

G) Almost there. The final step is to declare which files, project output to deploy when Installing and Uninstalling. Right-Click on the Setup Project again. And select View -> Custom Actions.

In this window, add the Primary output of your Service Project to the Install and Uninstall sections as shown in the image below:

H) Now it’s time to test this all out.

  • Build all the projects in the Solution
  • Right-Click your Setup Project
  • Select INSTALL – This should open up the Installation Wizard. Keep hitting NEXT until the Wizard ends.

Once installation is successful, your new Windows Service should be visible under Computer Management -> Services.

If it’s there:  Congratulations!  You have just programmed and installed your Windows Service 🙂

Conclusion:

This is the way to automate the installation of a Windows Service; and this is particularly useful if you perform deployment at your Client offices.  If installing locally, or internally and you have access to the Server with Administration privileges:  You can skip the Setup Project altogether and instead use the INSTALLUTIL that comes with .Net Framework for your manual installation.

Hope this has given you a better understanding to Windows Services  🙂