Create a Windows Service Application

Source: Internet
Author: User

This article is adapted from msdn

Create a Windows Service Application in the component designer

1. Create and configure a service

On the File menu, point to new and click Project ".
The Create Project dialog box is displayed.

In the list of Visual C # project templates, select the "Windows Service" project and name it mynewservice.
Note that the Project template automatically adds a component class named service1 that inherits from system. serviceprocess. servicebase.
Click designer to select service1. Then, in the "properties" window, set the servicename attribute of service1 to mynewservice.
Set the autolog attribute to true.
In the "View" menu, select "code" to open the code editor. Edit the main method to create an instance of mynewservice. When you rename a service in step 3

, The class name is not modified in the main method.

Static void main ()
{
System. serviceprocess. servicebase [] servicestorun;
// Change the following line to match.
Servicestorun = new system. serviceprocess. servicebase []
{New mynewservice ()};
System. serviceprocess. servicebase. Run (servicestorun );
}
Add the Custom Event Log to the Windows service. Event Logs are not associated with Windows Services in any form. The EventLog component can be used to add

Example of the component type added to the Windows service.

2. Add the Custom Event Log feature to the service.

In Solution Explorer, right-click service1.cs and select View designer ".
On the components tab of the Toolkit, drag the EventLog component to the designer.
In Solution Explorer, right-click service1.cs and select view code ".
Edit the constructor to define a user event log.

Public mynewservice ()
{
Initializecomponent ();
If (! System. Diagnostics. EventLog. sourceexists ("mysource "))
{
System. Diagnostics. EventLog. createeventsource (
"Mysource", "mynewlog ");
}
Eventlog1.source = "mysource ";
Eventlog1.log = "mynewlog ";
}
3. Define what happens when the service is started

In the Code Editor, locate the onstart method that is automatically rewritten when a project is created, and write code to determine what happens when the service starts running:

Protected override void onstart (string [] ARGs)
{
Eventlog1.writeentry ("in onstart ");
}
Note that the service application is designed to run for a long time. Therefore, it usually polls or monitors the situation in the system. Monitoring is set in the onstart method. However,

Onstart is not actually monitored. Once a service operation starts, the onstart method must be returned to the operating system. It cannot always loop or block. To set a simple Wheel

You can use the system. Timers. Timer component. In the onstart method, set the parameter on the component and set the timer. enabled attribute to true.

. Then, the timer will periodically trigger events in the Code. At this time, the service can perform its monitoring work.

4. Define what happens when the service is stopped

In the Code Editor, locate the onstop process that is automatically rewritten when a project is created, and write code to determine what happens when the service is stopped:

Protected override void onstop ()
{
Eventlog1.writeentry ("in onstop .");
}
You can also override the onpause, oncontinue, and onshutdown methods to define further processing of components.

5. Define other services

For the method to be processed, rewrite the appropriate method and define the operation to take place.
The following code shows how to rewrite the code of the oncontinue method:

Protected override void oncontinue ()
{
Eventlog1.writeentry ("in oncontinue .");
}
Some custom operations must occur when the Windows service is installed. This can be done by the installer class. Visual Studio can be created for Windows Services

Install these programs and add them to the project.

6. Create an installer for the service

Return to the "design" view of service1.
Click the background of the designer to select the service itself, instead of anything about it.
In the Properties window, click the add installer link in the gray area below the property list.
By default, add component classes that contain two installers to your project. Name the component projectinstaller, which contains the installation programs of the service

Install programs for processes associated with services.

Access the project Installer's design view and click serviceinstaller1 ".
In the "properties" window, set the servicename attribute to mynewservice.
Set the starttype attribute to automatic.
In the designer, select serviceprocessinstaller1 (for Visual Basic projects) or serviceprocessinstaller1 (for Visual C # items

). Set the account attribute to LocalService. This allows you to install and run the service on a local service account. For more information, see

Serviceprocessinstaller. Account attribute.
Security Description: the LocalService account is used as a non-privileged user on the Local Computer and displays anonymous creden。 to any remote server. Be especially careful when using other accounts,

Therefore, they have high privileges and will increase your risk of malicious code attacks.

7. Generate a Service Project

In Solution Explorer, right-click your project and select Properties From the shortcut menu ". The "property page" dialog box of the project appears.
In the left-side pane, select the General tab in the general attributes folder.
From the "launch object" list, select or mynewservice. mynewservice. Click OK ".
Press Ctrl + Shift + B to generate the project.
After a project is generated, you can deploy it. The installation project will install the compiled project file and run the installation program required to run the Windows service. To complete the creation

For the entire installation project, you need to add the project output mynewservice.exe to the installation project, and then add custom operations to install mynewservice.exe. For installation

For more information about the project, see the installation project. For more information about custom operations, see Walkthrough: create custom operations.

8. 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.

9. 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.

10. 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 ".

11. Install Windows Services

To install myeventlog.exe, right-click the installation project in Solution Explorer and select "Install ".

12. 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 "services and

Application 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 ".

13. Verify the event log output of the service

Open the server resource manager and access the "event log" node. For more information, see process event logs in server resource manager.

Find the list of mynewlog and expand it. You should see the items that the Service performs operations.
14. Uninstall the 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 restarted on the System

It is not completely deleted before startup.

Next step
You can also use the servicecontroller component to send commands to installed services.

You can use the installer to create event logs when installing the application, instead of creating event logs when running the application. In addition, when the application is uninstalled, the event log is deleted by the installer.

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.