. Net Windows service is automatically started after installation

Source: Internet
Author: User

For ease of deployment, we generally make Windows Services written in C # Into an installation package. After the service is installed, You need to manually start the service for the first time, which is inconvenient. I checked some information on the Internet and found that one method is to call the command line method to start the service in the installation completion event. This method is feasible but not perfect. Now let's take a look at how to better enable the service automatically.

1. Override the commit method of projectinstaller

 Using System; Using System. collections; Using System. Collections. Generic; Using System. componentmodel; Using System. configuration. Install; Using System. LINQ; Using System. serviceprocess; Namespace Cleanexpiredsessionseivice {[runinstaller ( True )]Public   Partial   Class Projectinstaller: system. configuration. Install. installer { Public Projectinstaller () {initializecomponent ();} Public   Override   Void Commit (idictionary savedstate ){ Base . Commit (savedstate ); // Auot start service after the installation is completed Servicecontroller SC = New Servicecontroller ( "Cleanexpiredsessionseivice" );If (SC. Status. Equals (servicecontrollerstatus. Stopped) {SC. Start ();}}}}

2. Add the custome action named commit to the service installation project.

Right-click the service installation project and choose View-custom actions from the shortcut menu.

      right-click the commit item and choose add custom action ..., Select application folder in the pop-up list box. The final result is as follows:

      note that the second step is essential. Otherwise, the service cannot be started automatically. My personal understanding is that commit custom action will automatically call the commit method of projectinstaller. Commit custom action plays a caller role here.

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.