vs2015 Developing Windows Services

Source: Internet
Author: User

Work has been a long time, so long after writing this article is to give their own reference later. It's not a long time to forget about the entire development process without writing Windows services. Windows service development, based on the not said, directly on the process.

1, the new Windows Service project, I chose here is Framework4.0, did not select the high version is to prevent the service on some low-version system does not work properly.

2. Add the installer for the Windows service.

In the default Service1 designer interface, right-click Add Installer, the system automatically creates a new installer class with the default configuration, such as:

After you have created the installer, you need to make some basic property settings for the default ServiceInstaller1 and ServiceProcessInstaller1. Such as:

The above work is completed, the installation program is complete.

Note: If you do not add the installer to the service, you cannot install the service into the Windows system.

3. Add the application configuration file (if necessary).

If the project is needed, some application configuration parameters can be set in this file (for example: Database connection string).

4. Write the Windows Service Master code

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.diagnostics;using system.linq;using system.serviceprocess;using system.text;using System.IO;namespace            organizclientsocketservice{public partial class Service1:servicebase {public Service1 () {            InitializeComponent ();            System.Timers.Timer Timer = new System.Timers.Timer (); Timer.            Elapsed + = new System.Timers.ElapsedEventHandler (timedevent); Timer. Interval = 5000;//Executes a timer every 5 seconds.        Enabled = true; }//Timed execution event private void Timedevent (object sender, System.Timers.ElapsedEventArgs e) {//Business Logic code} protected override void OnStart (string[] args) {this.        Writelog ("Search only Organiz Client data Synchronization service:" Service start "); } protected override void OnStop () {this.        Writelog ("Search only Organiz Client data Synchronization Service:" Service Stop "); } protected override void OnShutdown() {this.        Writelog ("Search only Organiz Client data Synchronization service:" Computer off "); } #region Log///<summary>///record Log///</summary>//<param name= "Ms            G "></param> private void Writelog (String msg) {//string path = @" C:\log.txt ";            The log file will exist in the Windows Service program directory under string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";            FileInfo file = new FileInfo (path); if (!file.                Exists) {FileStream fs;                FS = file.create (path); Fs.            Close (); } using (FileStream fs = new FileStream (path, Filemode.append, FileAccess.Write)) {u Sing (StreamWriter sw = new StreamWriter (FS)) {SW.                WriteLine (DateTime.Now.ToString () + "" + msg); }}} #endregion}}

5. Compile build, install Windows service to Windows system.

After the development is completed, the whole project is compiled and generated. In the Windows Service Development folder "\Bin\Debug", is the service we need to install, it is recommended to copy all the files inside the system to a directory to install.

I am copying the files in the whole folder to the C:\WindowService folder. Then open the directory C:\Windows\Microsoft.NET\Framework64\v4.0.30319, copy the InstallUtil.exe file inside to the C:\WindowService folder).

  Note: My system is windows10,64 bit system, my service will also install to 64-bit system, so I was into C:\Windows\Microsoft.NET\Framework64\ v4.0.30319 directory copy InstallUtil.exe file. When you install, according to the target system you installed, to feel that it is a copy of which version of the framework, whether the specific 64-bit or 32-bit is also determined by your system.

Do the above work can be installed, open the CDM can be installed (be sure to run as an administrator Yo, or the installation will be reported "Windows Service installation exception: System.Security.SecurityException: Source is not found, But failed to search for some or all of the events ").

The following are the installation commands, the start Service command, the Stop Service command, the Uninstall Service command:

Install command: C:\WindowService\InstallUtil.exe C:\WindowService\OrganizClientSocketService.exe

Start Service command: net start search Organiz client data Synchronization Service

Shutdown Service Command: net stop search Organiz client data Synchronization Service

Uninstall Service command: C:\WindowService\InstallUtil.exe-u C:\WindowService\OrganizClientSocketService.exe

vs2015 Developing Windows Services

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.