服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-05-30 1ae7b5a517aaa0f3a45f0b31b0c5173c35558318
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Rhea.Common;
using Tiger.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Tiger.IBusiness;
 
namespace Tiger.Business
{
    /// <summary>
    /// 事务基类
    /// </summary>
    public abstract class TransactionBase : IDisposable, ITransaction
    {
        /// <summary>
        /// 事务基类
        /// </summary>
        public TransactionBase()
        {
        }
        /// <summary>
        /// 事务ID
        /// </summary>
        public string TransID { get; set; }
        /// <summary>
        /// Api Host
        /// </summary>
        public string ApiHost { get; set; }
        /// <summary>
        /// Remote Address
        /// </summary>
        public string RemoteAddress { get; set; }
        /// <summary>
        /// 事务锁
        /// </summary>
        public object TransLock { get; } = new object();
        /// <summary>
        /// ApiAction历史记录
        /// </summary>
        protected Dictionary<string, ActionHistory> ActionHistoryList = new Dictionary<string, ActionHistory>();
        /// <summary>
        /// 最后操作的时间
        /// </summary>
        public DateTime LastActionTime { get; set; }
        /// <summary>
        /// 事务是否完成
        /// </summary>
        public bool IsFinished { get; set; }
 
        /// <summary>
        /// 添加一个ApiAction的历史记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <param name="action"></param>
        public virtual void AddHistory<T>(Microsoft.AspNetCore.Http.HttpRequest request, ApiAction<T> action)
        {
            try
            {
                ActionHistoryList.Add($"{request.HttpContext.TraceIdentifier} at {action.Timestamp:yyyy/MM/dd HH:mm:ss.fff}: {request.Path}", action.History());
            }
            catch (System.Exception ex)
            {
                Logger.Default.Fatal(ex, "添加ApiAction历史记录异常");
            }
            LastActionTime = DateTime.Now;
        }
 
        /// <summary>
        /// 保存ApiAction的历史记录到文件
        /// </summary>
        public virtual void SaveHistory()
        {
            DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\Transaction");
            if (!di.Exists)
            {
                di.Create();
            }
            var log = new FileInfo(di.FullName + @$"\{this.TransID}.log");
            File.AppendAllText(log.FullName, "{");
            File.AppendAllText(log.FullName, "\"Transaction Data\":");
            File.AppendAllText(log.FullName, JsonConvert.SerializeObject(this) + "\r\n");
            File.AppendAllText(log.FullName, ",\"Transaction History\":");
            File.AppendAllText(log.FullName, JsonConvert.SerializeObject(ActionHistoryList) + "\r\n");
            File.AppendAllText(log.FullName, "}\r\n");
        }
 
        /// <summary>
        /// 关闭事务
        /// </summary>
        /// <param name="needSaveHistoryLog"></param>
        /// <returns></returns>
        public virtual bool Close(bool needSaveHistoryLog = false)
        {
            if (needSaveHistoryLog)
            {
                SaveHistory();
            }
            Dispose();
            return true;
        }
 
        public virtual void Dispose()
        {
            BizContext.TransactionDic.Remove(TransID);
        }
    }
}