Windows Services Deployment and uninstallation

Source: Internet
Author: User
Tags least privilege

Colleagues asked about the Windows service, now tidy up, how to create a Windows service in C #, and how to debug, deploy, uninstall.

First, create a Windows service

1. Open VS2008, create a new project, project type Select Visual C#-windows, select Windows Service in templates, others can default, click OK.

2. In the Solution Explorer, you will see that three files are automatically generated: app. config, Program.cs,service1.cs, where app. config can add some custom configuration information for the code file to use; Program.cs provides the main () method, as the population of the program, generally does not need to modify Service1.cs is the service to be realized.

3. Double hit Open Service1.cs, will open by default Service1.cs[design], in this interface can drag a component, in addition, in the properties, you will see some of the service's property settings, such as AutoLog (True: Log the service using Windows event log; False: User can customize their own event log), CanPauseAndContinue (True: can handle pausing and continuing the operation of the service , False: Cannot handle pausing or continuing the operation of the service), ServiceName (used to let service Control Manager recognize the name of the services).

4. Go to Code view, you can see that a constructor is generated by default, and two methods that need to be overridden onstart and onstop, we need to override both methods. The following is a simple rewrite of the OnStart method.

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 (); This is the most basic of a Windows service, but whether it works, we need to debug one.

Second, debug Windows service

In general, the debugging method we use is to deploy the service and then attach to process ... to find the corresponding service process and debug it. However, the operation is cumbersome, after modifying the service, you need to uninstall the service, and then redeploy, and the OnStart method is difficult to debug. From the network to find a good debugging method, and try to pass, very useful, recommend to everyone:)

1. Add the public method to Service1.cs and encapsulate the protected method that you want to debug for the main () call to debug OnStart () as an example:

public void Start (string[] args) {this.   OnStart (args); }

2. Add the following code in main () to perform the 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&nbSp;is stopped ");             }              else              {                 servicebase[] servicestorun;                 servicestorun  = new ServiceBase[]          {            New service1 ()          };                  Servicebase.run (ServicesToRun);             }

3. Change the output type of project

Right-click the project, click Properties, and in the Application tab, change the Output type to console application.

OK, so you can press F5 to debug:)

III. deployment

1. In the Service1.cs Design view, right click on the mouse, click Add Installer, will automatically create a ProjectInstall.cs file, in the Design view, there are two components ServiceProcessInstaller1: This component has a property called Accout, the default is user, so that in the deployment of the need to enter the user name and password, here we can choose The least privilege of LocalService, of course, to get more permissions to set the other two network service and local

ServiceInstaller1: Information related to the service itself, such as service name, service description, display name, startup type, and so on.

2. If you use the system default event log, you can now compile and deploy it, and if you use a custom event log, you need to modify some of the code in ProjectInstaller.

3. Deploying Windows Service using Installutil.exe

( note the permission, here need to use Administrator user to deploy, otherwise will prompt some error message : For example an exception occurred during the Install phase.  System.Security.SecurityException:The source is not a found, but the some or all Eve NT logs could is not 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

This is where the deployment is done.

Iv. Uninstall Service

InstallUtil/ uD:\project\WindowsService1\WindowsService1\bin\Debug\windowsservice1.exe

Five, start the service

1. When starting the service, sometimes there will be a service start and stop after the error message, indicating that the service may not be used , we can view eventvwr, which will give enough error information, based on the error message to adjust the service code can be It is also possible because the service to manipulate the file, there is no permission for the reason, this time to operate the file permissions to release it.

2. When the service is started, we can set up the starting account because of the failure of the permission issue to start.

3. The parameters of Windows service are passed one time at startup, can be passed in as interface, or by command line: SC start Service1 arg0 arg1 ...

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.