How to create a Windowsservice

Source: Internet
Author: User

Creating a Windows service can be divided into the following steps:

1. Create a "Windows Service" Project

2. Set the properties of the service to determine the service's name and working mechanism

Property Set up
ServiceName The service name. The value of the ServiceName property must always match the name that is recorded in the Setup class. If you change this property, you must also update the ServiceName property of the Installer class.
CanStop True to indicate that the service will accept a request to stop running, false to prevent the service from stopping, and the Service Control Manager to disable menu options for stopping the service.
CanShutdown True indicates that the service wants to be notified when the computer running the service shuts down so that the OnShutdown procedure can be called.
CanPauseAndContinue True indicates that the service will accept a request to pause or resume running, false to prevent the service from pausing and continuing, and the Service Control Manager to disable the appropriate menu option for pausing or continuing the service.
Canhandlepowerevent True indicates that the service can handle notifications about a computer's power state change, and false instructs the service to be notified about these changes.
AutoLog True indicates that an information entry is written to the application's event log when the service is performing an operation; False indicates that this feature is disabled.

3. Access the Code Editor and fill in the required processing for the OnStart and OnStop procedures. (These two methods are essential in the service)

4. Rewrite all other methods for which you want to define the functionality. such as OnShutdown, OnContinue, OnPause, Onpowerevent and other methods.

5. Add the required installers for the service application.

Here's how to add a Setup program: Double-click the service for which you want to add the installation component, open its Design view

Then click the background of the designer to select the service itself instead of any of its contents, right-click when the designer has focus, and then click Add Installer.

A new class ProjectInstaller and two installation components ServiceProcessInstaller and ServiceInstaller are added to the project, and the service's property values are copied to the component.

For each service application, there are at least two installation components in the ProjectInstaller class: The ServiceInstaller component installs the process for all services in the project, ServiceProcessInstaller A component is an installer for each service that the application contains.

Click the ServiceInstaller component to verify that the value of the ServiceName property is the same as the value of the ServiceName property of the service itself. (Note: both must be consistent)

The Description property corresponds to the description in the Service Control Manager

The DisplayName property corresponds to the name in the Service Control Manager

The StartType property determines how to start the service, which has the following three options:

After the Manual service is installed, it must be started manually.
Automatic the service will start automatically every time the computer restarts.
The Disabled service could not be started.

Click the ServiceProcessInstaller component and set Account Attribute property value, you can determine the security context in which the service will run.

By default, the service runs in a different security context than the logged-on user. The service runs in the context of the default system account called LocalSystem , which gives the service the system resource access privileges that are different from those provided to the user. You can change this behavior by specifying a different user account to run the service. You can set the security context by manipulating the account property of the process that is running the service. This property allows you to set the service to one of the following 4 account types:

  • user , when the service is installed and running in the context of an account specified by a single user on the network, the type will cause the system to require a valid user name and password;

  • LocalService , which runs in the context of an account that acts as a non-privileged user on the local computer and displays anonymous credentials to any remote server;

  • LocalSystem , which runs in the context of an account that provides a variety of local privileges and displays computer credentials to any remote server;

  • NetworkService , which runs in the context of an account that acts as a non-privileged user on the local computer and displays computer credentials to any remote server.

6. build the project by selecting Build Solution from the Build menu.

7. Install the service. Unlike most projects created in Visual Studio, Windows service projects cannot run directly from the development environment by pressing F5. this is because the project cannot run until the services in the project must be installed.

There are two ways to install and uninstall services:

Manual Installation Services

1. Enter "CMD" in the Run to open the command line, and then go to the directory where the compiled service is located

2. Enter the following code on the command line:installutil Yourproject.exe

Note: If you are prompted to enter a command, InstallUtil is not an internal command, you can find the directory where Installutil.exe resides, and then configure its path to the environment variable. In general. NET Framework4, Installutil.exe is located in the following directory:

C:\Windows\Microsoft.NET\Framework\v4.0.30319

If you do not want to configure environment variables, there is a workaround, copy the Installutil.exe to the directory where the compiled service is located, and then install it from the command line.

The steps to manually uninstall the service are the same as the steps installed, except that the command has more than one parameter: installutil/u yourproject.exe

Loading and unloading services via batch script

Open Notepad, type the appropriate command, and save it as a bat format

Installation script:

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe Yourproject.exe
Pause

Unload script

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe/u Yourproject.exe
Pause

How to create a Windowsservice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.