Use C # To create and package Windows Services!
Use C # To create Windows Services)
Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in vs.net. Use Visual C #
Creating a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. This service writes
Text Information.
Step 1: create a service framework
To create a new Windows service, you can select the Windows Service option from the Visual C # project, give the project a new file name, and click OK.
You can see that the wizard adds the webservice1.cs class to the project file:
The meaning of each attribute is:
Ü autolog: whether to automatically write data to System Log Files
Ücanhandlepowerevent receives power events during the service
Ü whether the canpauseandcontinue Service accepts requests for suspending or resuming running the service
Ü whether the canshutdown Service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process
Ü whether the canstop Service accepts the stop operation request
Ü servicename service name
Step 2: add features to the service
In the. CS code file, we can see that there are two ignored functions onstart and onstop.
The onstart function is executed when the service is started, and the onstop function is executed when the service is stopped. Here, when the service is started and stopped, some text information is written to a text file. The Code is as follows:
Protected override void onstart (string [] ARGs)
{
Filestream FS = new filestream (@ "D:/mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );
Streamwriter m_streamwriter = new streamwriter (FS );
M_streamwriter.basestream.seek (0, seekorigin. End );
M_streamwriter.writeline ("mcwindowsservice: Service started" + datetime. Now. tostring () + "/N ");
M_streamwriter.flush ();
M_streamwriter.close ();
FS. Close ();
}
Protected override void onstop ()
{
Filestream FS = new filestream (@ "D:/mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );
Streamwriter m_streamwriter = new streamwriter (FS );
M_streamwriter.basestream.seek (0, seekorigin. End );
M_streamwriter.writeline ("mcwindowsservice: Service stopped" + datetime. Now. tostring () + "/N ");
M_streamwriter.flush ();
M_streamwriter.close ();
FS. Close ();
}
Step 3: add the installer to the Service Application
Visual Studio. NET comes with the installation component that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed, and notifies the Service Control Manager of the existence of the service.
To correctly install the service, you do not need to perform any special encoding in the installer. However, to add special features to the installation process, you may occasionally need to modify the content of the installation program.
To add an installer to a service application, follow these steps:
1: In the solution, access the design view of the service to which you want to add and install components.
2: In the Properties window, click Add installer Link
In this case, a new projectinstaller class and two installation components serviceprocessinstaller and serviceinstaller are added to the project, and the service attribute values are copied to the component.
3: To determine how to start the service, click the serviceinstaller component and set the starttype attribute to an appropriate value.
Ümanual must be started manually after the service is installed.
Ü automatic the service is automatically started every time the computer restarts.
Ü the disabled service cannot be started.
4: Change the account attribute of the serviceprocessinstaller class to LocalSystem.
In this way, no matter which user is logged on to the system, the service will always start.
Step 4: generate a service program
You can select generate from the generate menu to generate a project.
Do not run the project by pressing F5-you cannot run the service project in this way.
Step 5: Install the service
Access the directory of compiled executable files in the project.
Use the project output as a parameter to run installutil.exe from the command line. Enter the following code in the command line:
Installutil yourproject.exe
Uninstall Service
Use the project output as a parameter to run installutil.exe from the command line.
Installutil/u yourproject.exe
Http://www.cnblogs.com/laiwen/archive/2005/08/21/219590.html
Packaging:
Create an installation project for the service
On the "file" menu, point to "add project" and select "new project ".
In the project type pane, select the installation and deployment projects folder.
Select "Installation Project" in the "template" pane ". Name the project myservicesetup.
The installation project is immediately added to the solution.
Then, add the output mynewservice.exe of the Windows Service Project to the installation project.
Add mynewservice.exe to the installation project
In Solution Explorer, right-click "myservicesetup", point to "add", and select "project output ".
The "add project output group" dialog box appears.
Select "mynewservice" in the "project" box ".
In the list box, select "primary output" and click "OK ".
The main output project of mynewservice is added to the installation project. Now add a custom operation to install the mynewservice.exe file.
Add custom operations to the installation project
In Solution Explorer, right-click the installation project, point to "View", and select "Custom operations ".
The custom operation editor is displayed.
In the "Custom operations" editor, right-click the "Custom operations" node and select "add custom operations ".
The "select items in project" dialog box appears.
Double-click "application folder" in the list box to open it, select "main output from mynewservice (activity)", and click "OK ".
The master output is added to all four nodes in the Custom operation, namely, "Install", "Submit", "rollback", and "Uninstall ".
In Solution Explorer, right-click the myservicesetup project and select generate ".
Install Windows Services
To install myeventlog.exe, right-click the installation project in Solution Explorer and select "Install ".
Start and Stop services
Open the Service Control Manager by performing one of the following operations:
In Windows 2000 Professional, right-click my computer on the desktop and Click Manage ". On the "Computer Management" console, expand the "services and applications" node.
-Or-
On Windows 2000 Server, click Start, point to programs, click Administrative Tools, and then click Services ".
Note: in Windows NT 4.0, you can open this dialog box from the control panel.
Now we can see that mynewservice is listed in the "service" Area of the window.
Select your service from the list, right-click the service, and click Start ".
Right-click the service and click "stop ".
Verify service event log output
Open the server resource manager and access the "event log" node. For more information, see process event logs in server resource manager.
Note that the server resource manager "server" node is unavailable in Visual Basic and Visual C #. Net Standard Edition. For more information, see
Visual Basic Standard Edition or Visual C # Standard Edition.
Find the list of mynewlog and expand it. You should see the items that the Service performs operations.
Uninstall Service
On the Start Menu, open control panel, click add or delete programs, find your service, and click Delete ".
You can also right-click the program icon of the. MSI file and select "Uninstall" to uninstall the program.
Note: If the service is installed on Windows 2000, you must restart the system before you can reinstall the service. In Windows 2000, the service is not completely deleted before the system restarts.