Create a self-hosted Windows service program using Topself

Source: Internet
Author: User

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

First, inherit ServiceControl write an application service class:

 Internal classAppservice:servicecontrol {ILog log= Logmanager.getlogger (typeof(Appservice));  Public voidInitialize () {log. Info ("Initialize"); Console.WriteLine ("Engine starting ...."); Enginecontext.initialize (false); Console.WriteLine ("Engine started"); }         Public BOOLStart (Hostcontrol hostcontrol) {Console.WriteLine ("Hosts starting ....");                   Servicehosthelper.openwcfservice (); Console.WriteLine ("Hosts started"); return true; }         Public BOOLStop (Hostcontrol hostcontrol) {servicehosthelper.closewcfservice (); return true; }    }

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

Static voidMain (string[] args) {            Try{hostfactory.run (x={X.runaslocalsystem ();                    X.startautomatically (); //x.startmanually ();                    varDefaultName =Getdefaultservicename (); varConfig =DXNConfig.Current.AppServer; X.setservicename ("My service 1"); X.setdisplayname ("My service 1"); X.setdescription ("1 Description of my service"); X.service (Factory=                    {                        varService =NewAppservice (); Service.                        Initialize (); returnService;                });            }); }            Catch(Exception ex) {Try{Logmanager.getlogger (typeof(program)). Fatal ("AppServer Launch Error", ex); }                Catch                {                 }                //throw;            }        }}


These service name information can be modified.
OK, now compile, our program can be used when the service.

After that, you can install the service using the command line:

@echo off %~d0 CD %~dp0 CD.. REM ApplicationServer.exe install-servicename "ApplicationServer"-displayname "ApplicationServer"-description " XX Business Management System "ApplicationServer. exe install @echo start the service ... ApplicationServer Start Echo start end


Start the service:

Start "ApplicationServer"

Stop service:

sc stop "ApplicationServer"

Uninstall Service:

ApplicationServer.exe Uninstall

When you run the first batch file that starts the service, the following content is output:

Configuration result:[success] Name dev_dxn_applicationserver[success] DisplayName applicationserver[success] Description XXX Business Management system [Success] ServiceName applicationservertopshelf v3.0.105.0,.NETFramework v4.0.30319.34014The transaction installation is running. The installation phase of the installation is beginning. Installing DXN ApplicationServer Service is installing services Dxn_applicationserver...The service Dxn_applicationserver has been successfully installed. Creating EventLog Source in log application ApplicationServer...the Install phase has completed successfully and the commit phase is beginning. The commit phase has completed successfully. The transaction installation has 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,.NETFramework v4.0.30319.34014The applicationserver service is notinstalled.

The process is all done.

Create a self-hosted Windows service program using Topself

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.