Creating Windows Services
The document is archived and information here might be outdated

Creating Windows Services

Visual Studio .NET 2003

When you create a service, you can use a Visual Studio .NET project template called Windows Service. This template automatically does much of the work for you by referencing the appropriate classes and namespaces, setting up the inheritance from the base class for services, and overriding several of the methods you're likely to want to override.

Note   The Windows Service template is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.

At a minimum, to create a functional service you must:

  • Set the ServiceName property.
  • Create the necessary installers for your service application.
  • Override and specify code for the OnStart and OnStop methods to customize the ways in which your service behaves.

After you add installers to your application, the next step is to create a setup project that will install the compiled project files and run the installers needed to install your service. To create a complete setup project, you must add the service project's output to the setup project and then add a custom action to have your service installed. For more information on setup projects, see Setup Projects. For more information on custom actions, see Walkthrough: Creating a Custom Action.

To create a Windows Service application

  1. Create a Windows Service project.
    Note   For instructions on writing a service without using the template, see Writing Services Programmatically.
  2. In the Properties window, set the ServiceName property for your service.
    Note   The value of the ServiceName property must always match the name recorded in the installer classes. If you change this property, you must update the ServiceName property of installer classes as well.
  3. Set any of the following properties to determine how your service will function.
    CanStopTrue to indicate that the service will accept requests to stop running; false to prevent the service from being stopped.
    CanShutDownTrue to indicate that the service wants to receive notification when the computer on which it lives shuts down, enabling it to call the OnShutDown procedure.
    CanPauseAndContinueTrue to indicate that the service will accept requests to pause or to resume running; false to prevent the service from being paused and resumed.
    CanHandlePowerEventTrue to indicate that the service can handle notification of changes to the computer's power status; false to prevent the service from being notified of these changes.
    AutoLogTrue to write informational entries to the Application event log when your service performs an action; false to disable this functionality. For more information, see Logging Information About Services.

    Note   By default, AutoLog is set to true.

    Note   When CanStop or CanPauseAndContinue are set to false, the Service Control Manager will disable the corresponding menu options to stop, pause, or continue the service.
  4. Access the Code Editor and fill in the processing you want for the OnStart and OnStop procedures.
  5. Override any other methods for which you want to define functionality.
  6. Add the necessary installers for your service application. For more information, see Adding Installers to your Service Application.
  7. 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.
  8. Install the service. For more information, see Installing and Uninstalling Services.

See Also

Introduction to Windows Service Applications | Writing Services Programmatically | Adding Installers to your Service Application | Logging Information About Services | Starting Services | Specifying the Security Context for Services | Installing and Uninstalling Services | Walkthrough: Creating a Windows Service Application in the Component Designer

© 2016 Microsoft