C # Add an installer for Windows Services

Source: Internet
Author: User
Tags microsoft website
Recently, I have been engaged in Windows Services and have a lot of experience. I feel that permissions are definitely much more restrictive than general programs, but they are also more convenient. For example, the system is not blocked in the background, and user logon is not required. Haha, it's a long journey. Today, let's talk about how to create an installation package for the Windows service. Why install the installation package? Of course it is convenient. You don't need to call installutil every time. Also, it looks more formal.

Let's talk about it more. Let's take a look at how to do it. First, you must create a Windows service project. Everyone should know how to do this (Leave a message asking me if I don't understand it), and then "add installer" to the service ", 1: (this step is exactly the same as registering with installutil)

(Figure 1)

After adding the package, a serviceprocessinstal1 and serviceinstaller1 will be displayed. If you have two services in a project, there will be two serviceinstaller classes, 2:

(Figure 2)

Figure 3 shows the attribute page of serviceprocessinstal1, where account refers to the account used to run the service. If it is only used locally, you can select LocalSystem (Local System), which is the highest local permission, you can also choose to interact with the desktop. If you want security, select the one that suits you.

(Figure 3)

Figure 4 shows the attribute page of serviceinstaller2, where displayname is the name of the service displayed in the Windows Service Manager, and desciption is the description of the service. The following servicename is the internal name of the service. The most important thing is the following starttype, which refers to the Service Startup method, which is automatic, manual, or disabled.


(Figure 4)

Next is the focus of today's lecture. Click "file"> "add"> "new project" (5)

(Figure 5)

Select "other project type"> "installation and deployment" (6)

(Figure 6)

After the project is created, right-click the newly created installation project and choose View> File System (7)

(Figure 7)

In this case, the file system attribute is opened. As you can see, there are three file systems on the left of the screen, because we are a Windows service program and do not need to place icons in the Start Menu or desktop, so right-click "application folder"> "add"> "project output" (8)

(Figure 8)

In the displayed dialog box, select "primary output" and "Release XXX". This is determined by the Project attributes. Generally, select release any CPU. (9)

(Figure 9)

The system will automatically put the project output file, that is, the Windows Service Program's exe file and all referenced class library files in the middle of the box. To facilitate the installation, we add installutil.exe and two self-written batch files. The add method is similar to the above, just change "project output" to "File ".
Result 10:

(Figure 10)

Next, configure the installation process and let the installer automatically install and uninstall the Windows service.

Right-click the project and choose "View"> "Custom operations". 11:

(Figure 11)

The window on the left is changed to the custom operation view, right-click "Install"-> branch of the worker. Click it. On the properties page on the right, set arguments = "your Windows service file name" and 14 (note that a space is required before the file name)

(Figure 12)


(Figure 13)


(Figure 14)

In the same example, installutil.exe is also available in the submission and dismounting processes. However, in the attribute page, set arguments = "-u your Windows service file name". In this way, the installer can automatically install, register, and uninstall Windows Services. (15)

(Figure 15)

Next, we should compile the Windows Service and installation project at the same time. Right-click solution and select "properties" (16). In the displayed dialog box, click Generate after the installation project. (17)

(Figure 16)


(Figure 17)

Finally, right-click the solution and select "generate solution". At this time, Vs will compile both projects. Note that:The startup sequence of the project must be before the Windows Service Project and after the installation project.
 The following is the Microsoft website information (I did not take a picture of the above, and then found that there is a problem in step 12), follow the below to succeed!Create a Windows service installation project This section describes how to create a Windows service project and how to install a compiled installation project in Windows service. Create a Windows Service Project

  1. Start Microsoft Visual Studio.
  2. InFilePointNewAnd then clickProject.
  3. ClickProject TypeUnderVisual C # projectAnd then clickTemplateUnderWindows Services.

    Note:In Visual Studio 2005 or Visual Studio 2008, expandProject TypeUnderVisual C #, ClickWindowsAnd then clickTemplateUnderWindows Services.

  4. TypeNameEnter c: \ inLocationLogwriterservice in the text box. ClickOK.
  5. Right-click Solution ExplorerService1.csAnd then clickView code.
  6. OnstartUse the following code to replace the annotation in the event handler:
    EventLog.WriteEntry("My simple service started.");

    .

  7. In Solution Explorer, double-clickService1.cs.
  8. In the code editor window, right-clickDesign viewAnd then clickAttribute
  9. In the properties pane, clickAdd installerLink.
  10. In the properties paneServiceinstaller 1, ChangeServicenameService 1.
  11. In the design view, in the code editor window, clickServiceprocessinstaller 1.
  12. In the properties pane, change to LocalSystem (</A0>-LocalServiceAndNetworkServiceIs only available in Microsoft Windows XP .)AccountAttribute.
After you complete the steps in configuring the Windows Service Project in the previous section, follow the steps below to add and deploy the project package the service application, to install the service application, follow these steps:
  1. Add a new project to the logwriterservice project. To do this, follow these steps:

    1. Right-click Solution ExplorerSolution logwriterservice (Project 1)PointAdd, ClickCreate a project.
    2. ClickInstall and deploy a projectLowerProject TypeAnd then clickTemplateUnderInstallation Project.
    3. InNameIn the text box, type servicesetup.
    4. TypeLocationText in the C: \ box, and then clickOK.
  2. The notification package is a deployment project. To do this, follow these steps:
    1. Right-click Solution ExplorerServicesetup, PointingAddAnd then clickProject output
    2. InAdd project output GroupInProjectClickLogwriterservice
    3. ClickPrimary outputAnd then clickOK.
  3. Add only primary output for correct installation. To perform custom operations, follow these steps:
    1. Right-click Solution ExplorerServicesetup, PointingViewAnd then clickCustom operations
    2. Right-clickCustom operationsAnd then clickAdd custom operations.
    3. ClickApplication folderAnd then clickOK.
    4. ClickThe primary output is from logwriterservice (active)And then clickOK.

      Note:Primary outputDisplayed onInstall,Submit,RollbackAndUninstall.

  4. By default, no configuration is generated for the installation project. To generate a solution, use the following method:
    • Method 1

      1. Right-clickLogwriterserviceAnd then clickGenerate.
      2. Right-clickServicesetupAnd then clickGenerate.
    • Method 2
      1. InGenerateIn the menu, clickConfiguration Manager.
      2. Click to selectGenerateThe check box is servicesetup.
      3. Press F 7 to generate the entire solution. When a solution is generated, you can use a complete installation package for the service.
  5. To install the new service, right-clickServicesetupAnd then clickInstall.
  6. InServicesetupClickNext stepThree times. Note that the progress bar appears during service installation.
  7. Service Installation clickClose.
Verify
  1. Double-clickManagement toolsAnd double-clickService
  2. Right-clickService 1And then clickStart
  3. Use one of the following methods to verify the events recorded in the event log:
    • Method 1

      1. Double-clickManagement toolsAnd double-clickEvent Viewer.
      2. ClickApplication logsIn the left-side navigation pane.
    • Method 2
      1. Expand in server Resource ManagerServer,Computername,Event Log, ExpandApplications, And then expandService 1. Call againService 1Is a class, not a service name itself. Therefore,Service 1Used as the application name. (It is not within the range of custom names described in this article.
      2. Move the cursor to log entries. The second entry should read "start my simple service" from the top ".
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.