Use TopSelf to create a Windows service program from the host.

Source: Internet
Author: User

Use TopSelf to create a Windows service program from the host.

In the traditional Windows service development process, you need to add a service installation program, which writes the installation, start and stop the service and other logic. Now, using TopSelf can simplify this process.

First, inherit ServiceControl to write an application service class:

 internal class AppService : ServiceControl    {        ILog log = LogManager.GetLogger(typeof(AppService));              public void Initialize()        {            log.Info("Initialize");            Console.WriteLine("Engine starting....");            EngineContext.Initialize(false);                     Console.WriteLine("Engine started");        }        public bool Start(HostControl hostControl)        {            Console.WriteLine("Hosts starting....");            ServiceHostHelper.OpenWcfService();                   Console.WriteLine("Hosts started");            return true;        }        public bool Stop(HostControl hostControl)        {            ServiceHostHelper.CloseWcfService();            return true;        }    }

Then, in the main program, write the following code:

Static void Main (string [] args) {try {HostFactory. run (x => {x. runAsLocalSystem (); x. startAutomatically (); // x. startManually (); var defaultName = getdefaservicservicename (); var config = DXNConfig. current. appServer; x. setServiceName ("My service 1"); x. setDisplayName ("My services 1"); x. setDescription ("My service 1 Description"); x. service (factory => {var service = new AppService (); service. initialize (); return service ;}) ;}catch (Exception ex) {try {LogManager. getLogger (typeof (Program )). fatal ("AppServer launch error", ex) ;}catch {}// throw ;}}}


The service name information can be modified.
Now we can compile our program as a service.

Then, you can use the command line to install the service:

@ Echo off % ~ D0cd % ~ Dp0cd.. rem ApplicationServer.exe install-servicename "ApplicationServer"-displayname "ApplicationServer"-description "XX business management system" ApplicationServer.exe install @ echo startup service ...applicationserver.exe startecho startup is complete


Start the service:

sc start "ApplicationServer"

Stop Service:

sc stop "ApplicationServer"

Uninstall the service:

ApplicationServer.exe uninstall

The following content is output when you run the batch file of the first startup service:

Configuration Result: [Success] Name DEV_DXN_ApplicationServer [Success] DisplayName ApplicationServer [Success] Description XXX business management system [Success] ServiceName ApplicationServerTopshelf v3.0.105.0,. NET Framework handler is running transaction processing and installation. Starting the "installation" phase of installation. Installing DXN ApplicationServer service is Installing the service DXN_ApplicationServer... the service DXN_ApplicationServer is successfully installed. The EventLog source ApplicationServer... installation stage is successfully created in the log Application, and the submit stage is being started. The "Submit" phase has been completed successfully. Transaction Processing and installation have been completed. Start the service... configuration Result: [Success] Name DXN_ApplicationServer [Success] DisplayName ApplicationServer [Success] Description XX management system [Success] ServiceName ApplicationServerTopshelf v3.0.105.0 ,. NET Framework v4.0.30319.34014The ApplicationServer service is not installed.

The process is complete.

 

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.