VS2010 Creating the Windowsservice service

Source: Internet
Author: User

1. Create a new Windows service

2. Add Installer

This step is important after you have finished processing your business logic and need to add a installer to be installed by your Windows service.

Add Installer to vs

Right-click your service, select View Designer

Then right-click Add Installer in the View Designer view

So the installer is added well.

3. Set Service parameters

A ProjectInstaller.cs is automatically generated when installer is added, and there is a InitializeComponent method in this file, as follows

[CSharp]View Plaincopy
  1. Private void InitializeComponent ()
  2. {
  3.   This.serviceprocessinstaller1 = new System.ServiceProcess.ServiceProcessInstaller ();
  4.   This.serviceinstaller1 = new System.ServiceProcess.ServiceInstaller ();
  5.   //
  6.   ServiceProcessInstaller1
  7.   //
  8.   This.serviceProcessInstaller1.Password = null;
  9.   This.serviceProcessInstaller1.Username = null;
  10.   //
  11.   ServiceInstaller1
  12.   //
  13.   This.serviceInstaller1.ServiceName = "Service1";
  14.   //
  15.   ProjectInstaller
  16.   //
  17.   This. Installers.addrange (new system.configuration.install.installer[] {
  18.   This.serviceprocessinstaller1,
  19.   This.serviceinstaller1});
  20. }
  21.   This.serviceProcessInstaller1.Password = null;
  22.   This.serviceProcessInstaller1.Username = null;



To set the login password for the service, if you do not want to set the username password, you can also run the service with the local System account code as follows:

  



This.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;


Other settings can also be done here.
4. Installing and uninstalling Windows Services

After the finished Windows service is published is an EXE file, in order to enable the service on the machine used, we will use the tools provided by Microsoft InstallUtil Tool, the command line to install and uninstall the service.

InstallUtil tools in the directory: System disk: \windows\microsoft.net\framework\v4.0.30319, run cmd, enter

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil Xxxx.exe Enter to complete the installation of the WINDOWS service.

The unload is the input c:\windows\microsoft.net\framework\v4.0.30319\installutil/u xxxx.exe carriage return.

5. When running the Windowservice service after installation, it is sometimes possible to encounter the situation that the service is automatically down, in which case the EXE file at compile time is x64 or x86. So we use Mubuild manual build into anycpu EXE service, may be resolved.

VS2010 Creating the Windowsservice service

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.