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

Source: Internet
Author: User

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 ();
}
In this way, the most basic Windows service is done, but we need to debug whether the service can run 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. here you need to deploy them with the administrator user. Otherwise, some error messages will be prompted, such as 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/u d: \ project \ windowsservice1 \ windowsservice1 \ bin \ debug \ windowsservice1.exe

5. Start the service

1. when starting a service, an error message is displayed, indicating that the service may not be used. You can view eventvwr, which provides sufficient error information, you can adjust the Service Code according to the error message. It may also be because the service requires file operations and has no permissions. In this case, you can open the file permissions 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.