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