Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create an installation project for Windows service in C#Windows service program

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to create an installation project for C#Windows service in the Windows service program. I hope you will get something after reading this article. Let's discuss it together.

The origin of the C#Windows service program installation project: this article describes how to create an installation project for a Windows service application (formerly known as the NT service). To do this, you must first create a solution that contains a simple Windows service project and write the entry to its application log. Then you add an installer project to the solution to install the Window service. * *, you start the service. You can start the service by using the service item in the Administrative tools folder on the start menu.

The C#Windows service program creates an installation project for the Windows service

This section describes how to create a Windows service project and how to use a compiled setup project if you want to install the Windows service.

The creation of a C#Windows service program a Windows service project

1. Start Microsoft Visual Studio.

On the File menu, point to New, and then click Project.

3. Under Project Type, click Visual C# Project, and then click Windows Service under templates.

Notice that in Visual Studio 2005 or Visual Studio 2008, expand the Visual C# project type, click Windows, and then click Windows Services under templates.

4. Type LogWriterService name text box, and then type C:\ in the location text box. Click OK.

In solution Explorer, Service1.cs, right-click, and then click View Code.

6. In the OnStart event handler, replace and comment the following code:

EventLog.WriteEntry ("My simple service started.")

In solution Explorer, double-click Service1.cs.

In the Code Editor window, right-click Design View, and then click Properties

9. In the properties pane, click the add installer link.

10. In the ServiceInstaller 1 properties pane, change the ServiceName property to Service 1.

11. In the Code Editor window in Design view, click ServiceProcessInstaller 1.

In the properties pane, change the account properties to LocalSystem (: LocalService and NetworkService values are only available in Microsoft Windows XP).

Use a compiled installer project to install the Windows service

After completing the steps in the previous section to configure the Windows service project, follow these steps to add the deployment project packaged service application so that you can install the service application:

1. Add a new project to your LogWriterService project. To do this, follow these steps:

In solution Explorer, right-click the solution LogWriterService (Project 1), point to add, and then click New Project.

B, click install and deploy Project under Project Type, and then click setup Project under template.

In the name text box, type ServiceSetup.

D, type C:\ location text box, and then click OK.

2. Tell the contents of the deployment project to the package. To do this, follow these steps:

In solution Explorer, right-click ServiceSetup, point to add, and then click Project Output (Project output

B. Click LogWriterService in the add Project output Group dialog box

Click Primary output (main output), and then click OK.

3. Add only the main output for the correct installation. To add a custom action, follow these steps:

In solution Explorer, right-click ServiceSetup, point to the view, and then click Custom Action

B, right-click the custom action, and then click add Custom Action.

Click the main output LogWriterService (Active), and then click OK.

You will notice that the main output appears in installation, commit, rollback, and uninstall.

4. The installer project is not included in the build configuration by default. To build a C#Windows service program solution, use one of the following methods:

C#Windows service program method 1

Right-click LogWriterService, and then click generate.

Right-click ServiceSetup, and then click generate.

C#Windows Service Program method 2

On the build menu, click configuration Manager to build the entire solution.

Click to select the build check box as ServiceSetup.

Press F7 to build the entire solution. When building a solution, it must be available for the service to be a complete installation package.

5. To install the new service, ServiceSetup, right-click, and then click install.

6. In the ServiceSetup dialog box, click next three times. You will notice that a progress bar appears during service installation.

7. When installing the service, click close.

Using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; namespace LogWriterService {public class Service1: System.ServiceProcess.ServiceBase {/ < summary > / Required designer variable. / / < / summary > private System.ComponentModel.Container components = null; public Service1 () {/ / The Windows.Forms Component Designer must have this call. InitializeComponent (); / / TODO: Add any initialization after the InitComponent call} / / The main entry point for the process static void Main () {System.ServiceProcess.ServiceBase [] ServicesToRun; / / More than one user service may run in the same process. To add / / another service to this process, change the following line to / / create a second service object. For example, / ServicesToRun = New System.ServiceProcess.ServiceBase [] {new Service1 (), new MySecondUserService ()}; / / ServicesToRun = new System.ServiceProcess.ServiceBase [] {new Service1 ()}; System.ServiceProcess.ServiceBase.Run (ServicesToRun);} / / < summary > / Required method for Designer support-do not modify / the contents of this method with the code editor. / / < / summary > private void InitializeComponent () {components = new System.ComponentModel.Container (); this.ServiceName = "Service1";} / / < summary > / Clean up any resources that are being used. / / < / summary > protected override void Dispose (bool disposing) {if (disposing) {if (components! = null) {components.Dispose ();}} base.Dispose (disposing);} / / < summary > / Set things in motion so your service can do its work. / / < / summary > protected override void OnStart (string [] args) {EventLog.WriteEntry ("My simple service started.");} / / < summary > / Stop this service. / / < / summary > protected override void OnStop () {/ / TODO: Add code here to perform any tear-down necessary to stop your service. }}}

The C#Windows service program verifies that it works properly

1. in the control panel, double-click the administrative tools, and then double-click the service

2. Right-click Service 1, and then click start

3. Use one of the following methods to verify that an event is recorded in the event log:

C#Windows service program verification method 1

In the Control Panel, double-click the administrative tools, and then double-click the event Viewer.

B. in the left pane, click Application Log, and then find your service from the right pane in the event log.

C#Windows service program verification method 2

Expand Server, ComputerName, event Log, expand the application in Server Explorer (Server Explorer), and then expand Service 1. Remember that Service 1 is a class, not the name of the service itself. Therefore, Service 1 is used as the name of the application. (it is beyond the scope of this article on how to customize names.

B. Move the cursor over the log entry. You should read "my simple Service" from the second entry at the top.

After reading this article, I believe you have a certain understanding of "how to create an installation project for Windows service in the C#Windows service program". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report