Use Visual Studio 2015 Community to develop windows Services, 2015 community

Source: Internet
Author: User

Use Visual Studio 2015 Community to develop windows Services, 2015 community

I studied how to develop a Windows Service Program under. NET yesterday. I encountered some minor problems during this period. Here I will only write down my development process and precautions and share them with the majority of users ......

1. Basics

A Windows service is a program that runs on its own when the system starts. Windows Services can process services in the background without an interactive interface.

. NET requires several basic classes to develop Windows Services. serviceProcess. dll and System. configuration. install. in dll, there are ServiceBase, ServiceInstaller, ServiceProcessInstaller, and Installer. These classes are required to develop a simple Windows service.

  2. Use Visual Studio 2015 Commnuity to create a Windows Service Project

    

In Classic Desktop (Classic Desktop program), select the Windows Service project. The project file structure is as follows:

  

Double-click Service1.cs to enter the service design interface and set the ServiceName attribute (this attribute will be mentioned later, please note). This attribute is the identifier of the system-controlled Windows Service:

  

Here, I set the ServiceName attribute to Test. You can check the attribute panel and find that the attribute of the ServiceBase class is set here. The ServiceBase class is the base class for creating all Windows Services in. NET. When creating a new service class, it must be derived from ServiceBase.

Next, let's take a look at the code section of the Service1.cs file. We can see that there is a Service1 class inherited from the ServiceBase class, and there are two rewrite methods, OnStart and OnStop. The code in OnStart runs after the service is started, and the code in OnStop runs when the service is stopped. In addition, there are OnPause, OnContinue, and other methods. This is the Windows service1.exe file generated under the bin \ dubug folder of our compilation project. This file is the Windows service we created. Is it easy? However, if you think that the creation of the Windows service is over so far, it will be wrong. Double-click windowsservice1.exe and try ......

3. Install Windows Services

As you can see, it prompts us that we must install the Windows service before running it.

  

Installutil.exe is mentioned in the prompt. It is not useful here. To install the created Windows service, you must first create an installation file in the project,

  

Double-click the newly created Installer1.cs file to go to its design interface. Regardless of the design interface, we can directly go to the Code interface and see a class: Installer1, which inherits from

The Installer class in the System. Configuration. Install namespace. The Installer class is the base class of all custom installers in. NET.

First, we define our service process installation class (ServiceProcessInstaller) instance and service installation class (ServiceInstaller) instance in the Installer1 constructor.

Public Installer1 () {InitializeComponent (); ServiceProcessInstaller spi = new ServiceProcessInstaller (); spi. account = ServiceAccount. localSystem; // set the type of account under which the service runs. // you can create multiple ServiceInstaller instances ServiceInstaller si = new ServiceInstaller (); si. serviceName = "Test"; // System Operation Service Identifier, which must be the same as the ServiceName attribute value set in ServiceBase si. displayName = "Test Service"; // The service name displayed to the user, that is, the service name si displayed on the control panel. description = "service Description"; si. startType = ServiceStartMode. manual; // how the service is started. Set it to Manual here. // remember to add the created instance to the installation list. this. installers. add (si); this. installers. add (spi );}View Code

At this point, the basic information of the installation service has been filled in. Next, we need to override the two methods in the base class Installer:

// Note that the Install and Uninstall methods must be overwritten, and the methods corresponding to the base class must be called in the rewrite method, otherwise, problems may occur during the installation and uninstallation of the service. // The small Editor is that the installation and uninstallation are caused by the absence of methods in the base class. // out of this, the Commit, Rollback, and other methods are public. override void Install (IDictionary stateSaver) {base. install (stateSaver);} public override void Uninstall (IDictionary savedState) {base. uninstall (savedState );}View Code

  

Note that the feature RunInstaller (true) is used in the Installer1 class. If we set the feature parameter to false, the Installation Tool installutil will ignore this class, we will not install the specified service in this class when installing the service.

After completing the above steps, we will re-compile the project ......

In the previous article, we mentioned the InstallUtil.exe tool, which is used to install Windows Services compiled by. NET. Its path is C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 \ InstallUtil.exe. In this example, v4.0.30319is the .net' release number. To use a different .neteditor to write Windows Services, you must use the corresponding installutil.exe to install the SDK.

We can open the Command line or use the Command line tool that comes with VS. Here we use the Command line tool that comes with VS: Developer Command Prompt for VS2015 for installation.

Open the command line tool and enter the path of installutil.exe Windows Service Program (the personal information-related part is described here). Then press Enter.

  

For example, use the system command line tool to first export to the path where installutil.exe is located or specify its path in the command line.

After you press enter, you will find that the Windows service is installed, and then you will find a problem with the installation ......

  

The solution to this problem is quite simple, that is, to run the command line tool as an administrator (the editor took a long time to understand this truth ).

Run the command line as an administrator and run the installation process again. We can see that the installation process is divided into two steps: Installation and submission.

After the installation is complete, we can see the installed Service in the Service Manager of the control panel:

  

After the installation is complete, start the service. You can use the command line to start the service or start it in the Service Manager.

  

Did you notice? The service name displayed in the Service Manager of the control panel is the name specified by the DisplayName attribute, and the service name specified by the ServiceName attribute must be used when starting the service. Otherwise, the system prompts that the service name is invalid.

Command: net stop service name.

Services are uninstalled using the installutil/u service program path.

So far, all the work of developing a simple Windows Service in. NET has been completed ......

 

Related Article

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.