using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.WindowsServices; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Rhea.Common; using Tiger.IBusiness; using System.ServiceProcess; using Tiger.Api.DbCache; namespace Tiger.Api { public static class WebHostServiceExtensions { public static void RunAsApiService(this IWebHost host) { try { var webHostService = new ApiWebHostService(host); ServiceBase.Run(webHostService); } catch (System.Exception ex) { Logger.Console.Fatal(ex, "Windows Services Exception"); } } } internal class ApiWebHostService : WebHostService { public ApiWebHostService(IWebHost host) : base(host) { } protected override void OnStarting(string[] args) { Logger.Default.Info("Run Api as Windows Services"); base.OnStarting(args); } protected override void OnStarted() { base.OnStarted(); Logger.Default.Info("Api Service is started"); } protected override void OnStopping() { try { Logger.Default.Info("Stopping Api Service"); //关闭Api监控总线 DI.Resolve().StopMonitors(); //关闭服务总线 DI.Resolve().StopServices(); //关闭DB缓存自动更新 //DI.Resolve().StopAutoUpdate(); DbCacheBus.StopAutoUpdate(); //MQTTHelper.MQTTConn?.Close(); base.OnStopping(); } catch (System.Exception ex) { Logger.Default.Fatal(ex, "Api Service stop exception"); } } protected override void OnStopped() { base.OnStopped(); Logger.Default.Info("Api Service is stopped"); } protected override void OnShutdown() { base.OnShutdown(); Logger.Default.Info("Api Service is shut down"); } } }