How to: Write Services Programmatically
If you choose not to use the Windows Service project template, you can write your own services by setting up the inheritance and other infrastructure elements yourself. When you create a service programmatically, you must perform several steps that the template would otherwise handle for you:
To write a service programmatically
Create an empty project and create a reference to the necessary namespaces by following these steps:
In Solution Explorer, right-click the References node and click Add Reference.
On the .NET Framework tab, scroll to System.dll and click Select.
Scroll to System.ServiceProcess.dll and click Select.
Override any other methods you want to define custom processing for, and write code to determine the actions the service should take in each case.
Add the necessary installers for your service application. For more information, see How to: Add Installers to Your Service Application.
Build your project by selecting Build Solution from the Build menu.
Do not press F5 to run your project — you cannot run a service project in this way.
Create a setup project and the custom actions to install your service. For an example, see Walkthrough: Creating a Windows Service Application in the Component Designer.
Install the service. For more information, see How to: Install and Uninstall Services.