C # Windows Service Program Development Guide

Source: Internet
Author: User

Reprinted: http://sunday.blog.51cto.com/360410/94315

This article describes in detail how.. NET development of Windows service programs, and will briefly introduce some development debugging skills and post-deployment problems. IDE: vs2005, dotnetframework version: 2.0.50727 Windows service program is usually a program that runs for a long time in the OS background. 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 in vs based on. net Windows service. 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. After selecting files> New> Project> Windows service, 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 following code is used in the CS file:
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 to  // create a second service object. For example,  //  //   ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};  //    ServicesToRun = new ServiceBase[]  { new Service1() };  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

 

To put it simply, stop the current service and copy everything in the DEBUG directory to the bin directory. Execute the reverse installation operation. Execute the installation operation and 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. 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. event Viewer on my computer --> right-click Management --> System Tools --> Event Viewer this article ends with a general introduction to 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 more articles on Windows Services, Enterprise Services, and applications integrated with remoting technology:
Windows service is very good and powerful. I send emails every day and regularly back up website data through FTP every day. Windows Service is used.

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.