服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-12-06 9719a7f0ccbb70e4e51a93cbe1733d1424c16f6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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<IMonitorBus>().StopMonitors();
                //关闭服务总线
                DI.Resolve<IServicesBus>().StopServices();
                //关闭DB缓存自动更新
                //DI.Resolve<IDbCacheBus>().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");
        }
    }
}