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.