Skip to content

Latest commit

 

History

History
76 lines (49 loc) · 4.41 KB

how-to-write-services-programmatically.md

File metadata and controls

76 lines (49 loc) · 4.41 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Write Services Programmatically
See how to write services programmatically by setting up the inheritance and other infrastructure elements yourself.
03/30/2017
csharp
vb
services, creating
Windows Service applications, creating
3abbb2ec-78d2-41e6-b9f9-6662d4e2cdc7

How to: Write Services Programmatically

[!INCLUDE windows-service-disambiguation]

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:

  • You must set up your service class to inherit from the xref:System.ServiceProcess.ServiceBase class.

  • You must create a Main method for your service project that defines the services to run and calls the xref:System.ServiceProcess.ServiceBase.Run%2A method on them.

  • You must override the xref:System.ServiceProcess.ServiceBase.OnStart%2A and xref:System.ServiceProcess.ServiceBase.OnStop%2A procedures and fill in any code you want them to run.

To write a service programmatically

  1. Create an empty project and create a reference to the necessary namespaces by following these steps:

    1. In Solution Explorer, right-click the References node and click Add Reference.

    2. On the .NET Framework tab, scroll to System.dll and click Select.

    3. Scroll to System.ServiceProcess.dll and click Select.

    4. Click OK.

  2. Add a class and configure it to inherit from xref:System.ServiceProcess.ServiceBase:

    [!code-csharpVbRadconService#7] [!code-vbVbRadconService#7]

  3. Add the following code to configure your service class:

    [!code-csharpVbRadconService#8] [!code-vbVbRadconService#8]

  4. Create a Main method for your class, and use it to define the service your class will contain; userService1 is the name of the class:

    [!code-csharpVbRadconService#9] [!code-vbVbRadconService#9]

  5. Override the xref:System.ServiceProcess.ServiceBase.OnStart%2A method, and define any processing you want to occur when your service is started.

    [!code-csharpVbRadconService#10] [!code-vbVbRadconService#10]

  6. 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.

  7. Add the necessary installers for your service application. For more information, see How to: Add Installers to Your Service Application.

  8. Build your project by selecting Build Solution from the Build menu.

    [!NOTE] Do not press F5 to run your project — you cannot run a service project in this way.

  9. 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.

  10. Install the service. For more information, see How to: Install and Uninstall Services.

See also