C # VS 2010 creating, installing, Debugging Windows Services (Windows service)

Source: Internet
Author: User

Methods for creating multiple Windows services in one application and 1083 resolution error resolution

--------------------------------------------------------------------------------------

1. Create a Windows Service project file, new project---installed templates, Visual C #, Windows, select Windows Services in the right window

2. The system has set up a Service1.cs component for us, which we use when testing. If you want a new piece, right-click Project, add New item--Windows services.

3. On Service1.cs, press F7 (right-click to view the code) to open the code page.   Add the code for our test. Note: The following several methods in the STR variable, in order for us to debug a moment when setting breakpoints with!

Using System;

Using System.Diagnostics;

Using System.ServiceProcess;

Namespace WindowsService1

{

public partial class Service1:servicebase

{

Public Service1 ()

{

InitializeComponent ();

Initservice ();

}

<summary>///initialization of service Parameters///</summary>

private void Initservice ()

{

Base. AutoLog = false;

Base. CanShutdown = true;

Base. CanStop = true;

Base. CanPauseAndContinue = true;

Base.  ServiceName = "Service1"; This name is important, and setting inconsistencies will produce 1083 errors!

}

protected override void OnStart (string[] args)

{String str = "Service open"; }

protected override void OnStop ()

{String str = "Service stopped"; }

protected override void OnContinue ()

{String str = "Service continues to run"; Base.    OnContinue (); }

protected override void OnPause () {String str = "Service paused"; Base.    OnPause (); }   } }

4. Set up the service "installer" 4.1 double-click "Service1.cs" to open the View Designer for Service1.cs

4.2 Right-click anywhere in the View Designer and select "Add Installer"

4.3 Then the project will appear with a ProjectInstaller.cs component (if the installer was added for the first time)

4.4 Double-click ProjectInstaller.cs to open the ProjectInstaller View Designer

4.5 Locate the ServiceInstaller1 component, select and press the F4 key to set the component properties. description= "test service 1"; Displayname= "Service1"; servicename= "Service1";//This value must be set with the Windowsservice1.initservice () method Base.   ServiceName attribute one to. StartType is the service run type, which can be set according to your needs. (Manual: Manual start, AutoMatic for auto start)

4.6 Locate the ServiceProcessInstaller1 component, select and press the F4 key to set the component properties. Account= "LocalSystem";//Set to another property prompts for a user name and password when the service is turned on

4.7 This completes all the preparation for a service, and the following is the installation and testing work.

5. Build the component right-click Project and select Build.

6. Installing components

6.1 Using the InstallUtil.exe tool to register Component Services, the file location is in a different version of the framework. I'm using 4.0, so I'm in a position C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.

6.2 Open the Build directory, if the build succeeds there will be a WindowsService1.exe file

6.3 Add two Bat (batch files) to the build directory root to create a new text file for the installation and uninstallation service, open the file and add the following two lines of command C:\Windows\Microsoft.NET\Framework\v4.0.30319\ InstallUtil.exe WindowsService1.exe Pause After you close the text file, and then rename it to "Install the service. bat" file, note to modify the suffix named. bat!

Create a new text file, open the file and add the following two lines of command c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe/u WindowsService1.exe pause After you close the text file, rename it to "Uninstall the service. bat" file, and note to modify the suffix named. bat!

6.4 "Install the service. bat" file, install the Windows service

7. After successful installation, we need to open the service manually, because we just set serviceinstaller1.starttype for Manual. Windows Service Manager, find the service named Service1, right-click Properties. Click on the "Start" button

8.windows Commissioning

8.1 Back to the development environment, select Debug, attach to process, tick "show all user Processes"

8.2 Find the WindowsService1.exe process (if no click on the Refresh button), select and click the "Attach to Process" button.

8.3 Select the Service1.cs file, and then press F7 to open the Code view

8.4 Add debug breakpoint on all str variables 8.5 go back to Windows Service Manager, look for the Service1 service and select it, there is a "pause" button on the left. Click here to hit the breakpoint under the OnPause Method! OK, this is the purpose of the commissioning.

9. If you want to install multiple services at once, you need to follow 2~5 steps to add a few more services!

--------------------------------------------------------------------------------------

Problems and solutions after a successful registration, you encounter an error when you start the Windows service: 1083 Solution

1. Check whether the specified service is started in the Main () method

Servicebase[] ServicesToRun;

ServicesToRun = new servicebase[]

{New Service1 (), New Service2 ()//If you need to add this code to create multiple services};

Servicebase.run (ServicesToRun);

2. Ensure that the ServiceName property of the ServiceInstaller1 component in the ProjectInstaller.cs View Designer is consistent with the ServiceName property of the Service1.cs component

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.