Home » Useful Services » Writing windows service in c#

Writing windows service in c#

This article demonstrates how to create a simple Windows Service application in Visual Studio that writes messages to an event log. Here are the basic steps that you perform to create and use your service:

Creating a Service by using the Windows Service project template, and configure it. This template creates a class for you that inherits from System.ServiceProcess. ServiceBase and writes much of the basic service code, such as the code to start the service.

Adding Features to the Service for the OnStart and OnStop procedures, and override any other methods that you want to redefine.

Setting Service Status. By default, services created with System.ServiceProcess. ServiceBase implement only a subset of the available status flags. If your service takes a long time to start up, pause, or stop, you can implement status values such as Start Pending or Stop Pending to indicate that it’s working on an operation.

(Optional) Set Startup Parameters. specify default startup arguments, and enable users to override default settings when they start your service manually.

Access the Windows Service Control Manager and Starting and Running the Service.

Before you can run a Windows Service, you need to install it, which registers it with the Service Control Manager. You can add installers to your project that handle the registration details.

To create the installers for your service

In Solution Explorer. open the context menu for MyNewService.cs or MyNewService.vb. and then choose View Designer.

Click the background of the designer to select the service itself, instead of any of its contents.

Open the context menu for the designer window (if you’re using a pointing device, right-click inside the window), and then choose Add Installer.

Writing windows service in c# Service Control Manager

By default, a component class that contains two installers is added to your project. The component is named ProjectInstaller. and the installers it contains are the installer for your service and the installer for the service’s associated process.

In Design view for ProjectInstaller. choose serviceInstaller1 for a Visual C# project, or ServiceInstaller1 for a Visual Basic project.

In the Properties window, make sure the ServiceName property is set to MyNewService .

Set the Description property to some text, such as “A sample service”. This text appears in the Services window and helps the user identify the service and understand what it’s used for.

Set the DisplayName property to the text that you want to appear in the Services window in the Name column. For example, you can enter “MyNewService Display Name”. This name can be different from the ServiceName property, which is the name used by the system (for example, when you use the net start command to start your service).

In the designer, choose serviceProcessInstaller1 for a Visual C# project, or ServiceProcessInstaller1 for a Visual Basic project. Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.

The LocalSystem account has broad permissions, including the ability to write to the event log. Use this account with caution, because it might increase your risk of attacks from malicious software.

For other tasks, consider using the LocalService account, which acts as a non-privileged user on the local computer and presents anonymous credentials to any remote server. This example fails if you try to use the LocalService account, because it needs permission to write to the event log.

A Windows Service, like any other executable, can accept command-line arguments, or startup parameters. When you add code to process startup parameters, users can start your service with their own custom startup parameters by using the Services window in the Windows Control Panel. However, these startup parameters are not persisted the next time the service starts. To set startup parameters permanently, you can set them in the registry, as shown in this procedure.

Before you decide to add startup parameters, consider whether that is the best way to pass information to your service. Although startup parameters are easy to use and to parse, and users can easily override them, they might be harder for users to discover and use without documentation. Generally, if your service requires more than just a few startup parameters, you should consider using the registry or a configuration file instead. Every Windows Service has an entry in the registry under HKLM\System\CurrentControlSet\services. Under the service’s key, you can use the Parameters subkey to store information that your service can access. You can use application configuration files for a Windows Service the same way you do for other types of programs. For example code, see AppSettings .

Adding startup parameters

In the Main method in Program.cs or in MyNewService.Designer.vb, add an argument for the command line:


Share this:
custom writing low cost
Order custom writing
Order custom writing
Important Notice!