Windows Service Program Development Guide under. net.

Source: Internet
Author: User

The last month was very busy, and the project progress was very tight. Let's look back at our blog. There was no article in May.ArticleI am very embarrassed. I will add a few articles to introduce the technologies I have recently followed. This article will introduce Windows Services.ProgramDevelopment.
Abstract:
This article will introduce in detail how to develop a Windows Service Program under. net, and briefly introduce some debugging skills in development and subsequent deployment problems.
IDE: vs2005, dotnetframework version: 2.0.50727

A Windows service program is usually a program that runs on the OS background for a long time. You can set it to start at the same time as the server starts, in traditional C/S program development, Windows Services are more common on the server. in. in the. NET application, whether it is an application in winform or ASP. net, Windows Services are also very important, for example, we can put. net remoting server is encapsulated in a Windows Service to provide stable services.

First, let's get to know how to develop. Net-based Windows Services in.
In fact, it is very simple to create a Windows Service Program under vs. Microsoft helped us do 80% of the work. All we need to do is to process 20% of the business work.
First, we create a Windows service program. vs provides a ready-made Project template. Choose files> New> Project> Windows service.

Then, you will get two file service components and program. CS. to create a service, you must first add a dedicated installation component. This step is also very simple. Select the service component and switch to the design mode. Right-click and choose add installer to generate the installation file, switch to the design mode. here you will see two components: serviceinstaller and processinstaller. There is not much to note here, most of which are about setting the service name, description, and other information, there are only two places to note. one is starttype. There are three options: automatic, manual, and disabled. if You Want To Enable Automatic startup, select automatic once. another attribute that needs to be noted is account. Select User. Generally, select local system.

Now, if we want to create a service, it is actually over. We only need to add personal business processing in the onstart and onstop functions of the Service component. in program. the CS file is as follows:Code:

Static   Void Main ()
{
Servicebase [] servicestorun;

// More than one user service may run within the same process. To add
// Another service to this process, change the following line
// Create a second service object. For example,
//
// Servicestorun = new servicebase [] {New service1 (), new myseconduserservice ()};
//
Servicestorun =   New Servicebase [] {NewService1 ()} ;

Servicebase. Run (servicestorun );
}

This code is used to start the service.

The next step is to install the service. You can create an installation package or directly execute the installation through the command line.

After you compile the project, you can. this program is located at % SystemRoot % \ Microsoft. net \ framework \/% version % \, version indicates. net version.
To install a service program, enter installutil.exe service program cmd.if you want to delete a service, it is the path of the installutil.exe/u service program.
After the installation is performed, go to control panel edition> Management Tools> services to view our service programs.

 

Finally, let's talk about the debugging skills.
Compiling such a service program is very troublesome to debug, and after each change, you have to frequently execute installutil and installutil/u commands. In fact, we can write some buildevents to greatly simplify the cumbersome work. here, we compile the following events in buildevents:
Net stop yourservicename
Copy/y "$ (targetdir)" $ (projectdir) bin"
"% SystemRoot % \ Microsoft. NET \ framework \ v2.0.50727 \ installutil.exe"/U $ (projectdir) bin \ $ (targetfilename)
"% SystemRoot % \ Microsoft. NET \ framework \ v2.0.50727 \ installutil.exe" $ (projectdir) bin \ $ (targetfilename)
Net start yourservicename
Below is a simple explanation:
Stop the current service-> copy everything in the DEBUG directory to the bin directory-> execute the reverse installation operation-> execute the installation operation-> start the service.
Finally, set the event to onsuccessfulbuild.

Because it is a service program, if you want to debug it directly, you can debug most of the Code by appending the process, but if you want to debug the onstart and onstop methods. debugging can only be performed through other projects. for details, refer to the introduction in this article. http://leonkim.bokee.com/1857046.html
In fact, we can directly use EventLog in the onstart () method. writeentry ("Hi, I'm Wiseman"); to write some information you are interested in to Event Viewer, which can solve the debug problem to a large extent.

This article ends with a general introduction of how. net, and some related skills. this article is also the knowledge I Need To Know after reading some articles. This month I will try to write a few more articles on Windows Services, Enterprise Services, and applications integrated with remoting technology.

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.