Use vs (C #) to create and debug Windows Services and deploy and uninstall Windows Services.

Source: Internet
Author: User
Tags eventvwr
Go to http://www.cnblogs.com/limei/archive/2010/07/06/1772332.htmluse vs (C #) to create, debug Windows service, and deploy and uninstall it.

My colleague asked about the Windows Service stuff. Now I want to sort it out, use C # To create a Windows service, and debug, deploy, and uninstall it.

1. Create a Windows Service

1. Open vs2008, create a project, select Visual C #-windows for the project type, select Windows service for the templates, and click OK for others.

2. In Solution Explorer, three files are automatically generated: App. config, program. CS, and service1.cs. App. config can add custom configuration informationCodeFile Usage; program. CS provides the main () method,ProgramGenerally, this parameter does not need to be modified. service1.cs is the service to be implemented.

3. double-click service1.cs to open service1.cs [design] by default. You can drag a component on this interface. In addition, some attribute settings about this service are displayed in properties, for example, autolog (true: use Windows event log to record the Service Log; false: You can customize your own event log), canpauseandcontinue (true: you can pause or continue the service. False: you cannot pause or continue the service. servicename is the name of the Service recognized by Service Control Manager ).

4. Go to the Code view and we can see that a constructor is generated by default, and two methods onstart and onstop to be rewritten are generated. We need to override these two methods. The onstart method is simply rewritten below.

Protected override void onstart (string [] ARGs) {string Path = @ "D: \ share \ test.txt"; streamwriter SW; Sw = new streamwriter (PATH ); foreach (string ARG in ARGs) SW. writeline (ARG); Sw. close (); Sw. dispose ();} is the most basic Windows service, but debugging is required if it runs normally.

Ii. debug Windows Service

Generally, the debugging method we use is to deploy the service, attach to process..., find the corresponding service process, and debug it. However, the operation is cumbersome. After the service is modified, You Need To uninstall the service and redeploy the service, and the onstart method is difficult to debug. Find a good debugging method on the network and try it. It is very useful. We recommend it to you :)

1. Add the public method to service1.cs and encapsulate the protected method to be debugged for calling Main (). Take onstart () as an example:

Public void start (string [] ARGs) {This. onstart (ARGs );}

2. Add the following code in main () and execute it during Human-Computer Interaction:

If ( Environment. userinteractive ) {Service1 S = new service1 (); string [] ARGs = {"A", "B"}; S. start (ARGs); console. writeline ("the service is started"); console. readline (); S. stop (); console. writeline ("the service is stopped");} else {servicebase [] servicestorun; servicestorun = new servicebase [] {New service1 ()}; servicebase. run (servicestorun );}

3. Change the output type of the project.

Right-click the project and click Properties. In the application tag, set output type to console application.

Okay, so that you can press F5 for debugging :)

Iii. Deployment

1. in the design view of service1.cs, right-click and click Add Installer. A project install is automatically created. CS file. In the design view, there are two components serviceprocessinstaller1: this component has an attribute named accout, which is a user by default. In this way, you need to enter the user name and password during deployment, here we can select the LocalService with the minimum permission. Of course, to obtain more permissions, we can set the other two network services and local

Serviceinstaller1: information related to the service, such as the service name, service description, display name, and Startup type.

2. If you use the default event log, you can compile and deploy it now. If you use a Custom Event Log, You need to modify some code in projectinstaller.

3. Use installutil.exe to deploy Windows Service

(Pay attention to permissions. You need to deploy the service as administrator. Otherwise, an error message is displayed.: For example, an exception occurred during the install phase. system. security. securityexception: the source was not found, but some or all Eve nt logs cocould not be searched. inaccessible logs: security. or access is denied)

RunAs/User: Administrator cmd

Cd c: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727

Installutil D: \ project \ windowsservice1 \ windowsservice1 \ bin \ debug \ windowsservice1.exe

Now the deployment is complete.

4. Uninstall the service

Installutil/UD: \ project \ windowsservice1 \ windowsservice1 \ bin \ debug \ windowsservice1.exe

5. Start the service

1. When starting the service, sometimes it appearsError message indicating that the service may not be used, We can view eventvwr, which will provide enough error information to adjust the Service Code according to the error information. It may also be because the service needs to operate.CompositionFile, because there is no permission, this time you can open the file permission to operate on it.

2. When starting the service, the startup failed due to permission issues, we can set the startup account.

3. Parameters of Windows Service are transmitted once at startup. parameters can be passed in the form of an interface, or in the form of a command line: SC start service1 arg0 arg1...

 

Reference link:

Http://nuigroup.com/forums/viewthread/9829/

Http://stackoverflow.com/questions/593454/easiest-language-to-create-a-windows-service/593803#593803

Http://topic.csdn.net/u/20090313/13/7c32b0e3-6e13-4226-8da4-392e31514454.html

Http://www.cnblogs.com/guoyinghai/archive/2008/04/16/1156943.html

My colleague asked about the Windows Service stuff. Now I want to sort it out, use C # To create a Windows service, and debug, deploy, and uninstall it.

1. Create a Windows Service

1. Open vs2008, create a project, select Visual C #-windows for the project type, select Windows service for the templates, and click OK for others.

2. in Solution Explorer, three files are automatically generated: app. config, program. CS, service1.cs, where app. config allows you to add custom configuration information for use in code files. program. CS provides the main () method, which generally does not need to be modified as the program population. service1.cs is the service to be implemented.

3. double-click service1.cs to open service1.cs [design] by default. You can drag a component on this interface. In addition, some attribute settings about this service are displayed in properties, for example, autolog (true: use Windows event log to record the Service Log; false: You can customize your own event log), canpauseandcontinue (true: you can pause or continue the service. False: you cannot pause or continue the service. servicename is the name of the Service recognized by Service Control Manager ).

4. Go to the Code view and we can see that a constructor is generated by default, and two methods onstart and onstop to be rewritten are generated. We need to override these two methods. The onstart method is simply rewritten below.

Protected override void onstart (string [] ARGs) {string Path = @ "D: \ share \ test.txt"; streamwriter SW; Sw = new streamwriter (PATH ); foreach (string ARG in ARGs) SW. writeline (ARG); Sw. close (); Sw. dispose ();} is the most basic Windows service, but debugging is required if it runs normally.

Ii. debug Windows Service

Generally, the debugging method we use is to deploy the service, attach to process..., find the corresponding service process, and debug it. However, the operation is cumbersome. After the service is modified, You Need To uninstall the service and redeploy the service, and the onstart method is difficult to debug. Find a good debugging method on the network and try it. It is very useful. We recommend it to you :)

1. Add the public method to service1.cs and encapsulate the protected method to be debugged for calling Main (). Take onstart () as an example:

Public void start (string [] ARGs) {This. onstart (ARGs );}

2. Add the following code in main () and execute it during Human-Computer Interaction:

If ( Environment. userinteractive ) {Service1 S = new service1 (); string [] ARGs = {"A", "B"}; S. start (ARGs); console. writeline ("the service is started"); console. readline (); S. stop (); console. writeline ("the service is stopped");} else {servicebase [] servicestorun; servicestorun = new servicebase [] {New service1 ()}; servicebase. run (servicestorun );}

3. Change the output type of the project.

Right-click the project and click Properties. In the application tag, set output type to console application.

Okay, so that you can press F5 for debugging :)

Iii. Deployment

1. in the design view of service1.cs, right-click and click Add Installer. A project install is automatically created. CS file. In the design view, there are two components serviceprocessinstaller1: this component has an attribute named accout, which is a user by default. In this way, you need to enter the user name and password during deployment, here we can select the LocalService with the minimum permission. Of course, to obtain more permissions, we can set the other two network services and local

Serviceinstaller1: information related to the service, such as the service name, service description, display name, and Startup type.

2. If you use the default event log, you can compile and deploy it now. If you use a Custom Event Log, You need to modify some code in projectinstaller.

3. Use installutil.exe to deploy Windows Service

(Pay attention to permissions. You need to deploy the service as administrator. Otherwise, an error message is displayed.: For example, an exception occurred during the install phase. system. security. securityexception: the source was not found, but some or all Eve nt logs cocould not be searched. inaccessible logs: security. or access is denied)

RunAs/User: Administrator cmd

Cd c: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727

Installutil D: \ project \ windowsservice1 \ windowsservice1 \ bin \ debug \ windowsservice1.exe

Now the deployment is complete.

4. Uninstall the service

Installutil/UD: \ project \ windowsservice1 \ windowsservice1 \ bin \ debug \ windowsservice1.exe

5. Start the service

1. When starting the service, sometimes it appearsError message indicating that the service may not be used, We can view eventvwr, which will provide enough error information to adjust the Service Code according to the error information. It may also be because the service has to operate files and has no permissions, at this time, you can open the file permission to be operated.

2. When starting the service, the startup failed due to permission issues, we can set the startup account.

3. Parameters of Windows Service are transmitted once at startup. parameters can be passed in the form of an interface, or in the form of a command line: SC start service1 arg0 arg1...

 

Reference link:

Http://nuigroup.com/forums/viewthread/9829/

Http://stackoverflow.com/questions/593454/easiest-language-to-create-a-windows-service/593803#593803

Http://topic.csdn.net/u/20090313/13/7c32b0e3-6e13-4226-8da4-392e31514454.html

Http://www.cnblogs.com/guoyinghai/archive/2008/04/16/1156943.html

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.