Development of Windows Services with. net

Source: Internet
Author: User
Tags log4net

1. What is Windows service?
Windows service applicationsProgramIt is an application that requires long-term running. It is especially suitable for the server environment. It does not have a user interface and does not produce any visual output. Any user message is written into the Windows event log. When the computer starts, the service automatically starts running. They do not run only when users log on. They can run in any user environment including the system. Through the Service Control Manager, Windows Services are controllable and can be terminated, paused, and started as needed.

2. Create a Windows Service
The service we are about to create is used to monitor whether the file is changed. As long as the file under the specified directory is changed, it will be recorded in the log. Next let's create this Windows service.

Visual Studio. NET makes creating a Windows Service quite simple. (1) Open visual studio.net
(2) create a Windows Service Project, select the "Visual C #" project, and then select the Windows Service Template
(3) set the project name to watchfileservice and click OK.
(4) Rename service1.cs to watchfile. CS
(5) Go to watchfile. CS Design Screen, set the servicename attribute in the Properties window to watchfileservice, Drag and Drop filesystemwatcher and EventLog from the tool --> component to the design screen, and drag and drop the results,

(6) set the filter attribute of filesystemwatcher1 to *. txt, and the path attribute to c: \ temp.
(7) set the source attribute of eventlog1 to fileSystem-Watcher1
(8) double-click filesystemwatcher1 and add the followingCode:

Private   Void Filesystemwatchersponchanged ( Object Sender, system. Io. filesystemeventargs E)
{
Eventlog1.writeentry (E. Name+ "This file is in:"+Datetime. Now. tostring ()+"Changed !");
}

3. Use the installer class
(1) Switch watchfile. CS to the design screen, and press the "add installer" option under the watchfile Properties window. Vs.net automatically adds a project Installer. CS file. The design screen of this file contains two controls: serviceprocessinstaller1 and serviceinstaller1.

(2) set the account attribute of the serviceprocessinstaller1 control to "LocalSystem"
(3) compile the project and find the watchfileservice.exe file in the bin \ debugdirectory of the project.

3. Install Windows Services
(1) enter the command prompt mode, and choose Start> program> Micosoft visual sdudio. net2003> visual sdudio.net tool> visual sdudio.net 2003.
(2) Go to the project Folder \ bin \ DEBUG directory at the command prompt.
(3) Input installutil watchfileservice.exe. The service is successfully installed.

(4) to delete this service, enter installutil/u watchfileservice.exe

4. Set the service to auto start
Change projectinstaller. CS to the design status and modify the starttype attribute of serviceinstaller1 to automatic. After the service is installed, the service starts automatically after the operating system restarts.

5. debug Windows Services
From another perspective, debugging Windows Services is totally different from a common application. More steps are required to debug Windows Services. Services cannot be debugged as long as they are simply executed in the development environment as you do for common applications. The service must be installed and started first. We have done this in the previous section. To facilitate code tracing and debugging, once the service is started, you must use Visual Studio to append the running process (attach ). Remember, any modifications made to your Windows service must be uninstalled and reinstalled.

6. append the running Windows service.
To debug the program, some operating instructions for the Windows Service are attached. These operations assume that you have installed the Windows service and it is running.

(1) Use Visual Studio to load this project
(2) Click the "debug" menu
(3) Click the process menu.
(4) ensure that the system process is selected
(5) In the list of available processes, locate the process on your Executable File Name and click to select it
(6) Click the Add button.
(7) Click OK.
(8) Click Close.
(9) set a breakpoint in the filesystemwatcher1_changed method and wait for it to execute

7. Effect
(1) create a text file in the C: \ Temp directory and edit the file
(2) Open the application log in the Event Viewer tool and you can see that a log project is generated,

Posted on Zhenhe reading (1266) Comments (12) EDIT favorite to 365key category: Web and Windows Service
Comment:

# Re: Using. Net to develop Windows Services | you and I are doing similar things. The problem I encountered was that I used log4net for logs. If you write a normal EXE, it will be good. But once written as a service, the log does not reflect it.
Hope to have more communication. Thank you very much for trying to use log4net in the service.
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.