升级.net8
This commit is contained in:
@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@ -13,24 +14,39 @@ using HT.Cloud.Code.Model;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
public class GlobalContext
|
||||
public static class GlobalContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务集合
|
||||
/// </summary>
|
||||
public static IServiceCollection Services { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根服务
|
||||
/// </summary>
|
||||
public static IServiceProvider RootServices { get; set; }
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
static GlobalContext()
|
||||
{
|
||||
// 未托管的对象
|
||||
UnmanagedObjects = new ConcurrentBag<IDisposable>();
|
||||
}
|
||||
/// <summary>
|
||||
/// 服务集合
|
||||
/// </summary>
|
||||
public static IServiceCollection Services { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 根服务
|
||||
/// </summary>
|
||||
public static IServiceProvider RootServices { get; set; }
|
||||
|
||||
public static IConfiguration Configuration { get; set; }
|
||||
|
||||
public static IWebHostEnvironment HostingEnvironment { get; set; }
|
||||
public static HttpContext HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext;
|
||||
|
||||
public static SystemConfig SystemConfig { get; set; }
|
||||
/// <summary>
|
||||
/// 未托管的对象集合
|
||||
/// </summary>
|
||||
public static readonly ConcurrentBag<IDisposable> UnmanagedObjects;
|
||||
|
||||
public static HttpContext HttpContext => RootServices?.GetService<IHttpContextAccessor>()?.HttpContext;
|
||||
|
||||
public static SystemConfig SystemConfig { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求生存周期的服务(未注册返回null)
|
||||
@ -91,27 +107,70 @@ namespace HT.Cloud.Code
|
||||
{
|
||||
return RootServices;
|
||||
}
|
||||
if (HttpContext?.RequestServices != null)
|
||||
{
|
||||
return HttpContext.RequestServices;
|
||||
}
|
||||
// 第二选择是获取 HttpContext 对象的 RequestServices
|
||||
var httpContext = HttpContext;
|
||||
if (httpContext?.RequestServices != null) return httpContext.RequestServices;
|
||||
// 第三选择,创建新的作用域并返回服务提供器
|
||||
else if (RootServices != null)
|
||||
{
|
||||
using var scoped = RootServices.CreateScope();
|
||||
var scoped = RootServices.CreateScope();
|
||||
UnmanagedObjects.Add(scoped);
|
||||
return scoped.ServiceProvider;
|
||||
}
|
||||
// 第四选择,构建新的服务对象(性能最差)
|
||||
else
|
||||
{
|
||||
using var serviceProvider = Services.BuildServiceProvider();
|
||||
var serviceProvider = Services.BuildServiceProvider();
|
||||
UnmanagedObjects.Add(serviceProvider);
|
||||
return serviceProvider;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// GC 回收默认间隔
|
||||
/// </summary>
|
||||
private const int GC_COLLECT_INTERVAL_SECONDS = 5;
|
||||
|
||||
/// <summary>
|
||||
/// 记录最近 GC 回收时间
|
||||
/// </summary>
|
||||
private static DateTime? LastGCCollectTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 释放所有未托管的对象
|
||||
/// </summary>
|
||||
public static void DisposeUnmanagedObjects()
|
||||
{
|
||||
foreach (var dsp in UnmanagedObjects)
|
||||
{
|
||||
try
|
||||
{
|
||||
dsp?.Dispose();
|
||||
}
|
||||
finally { }
|
||||
}
|
||||
|
||||
// 强制手动回收 GC 内存
|
||||
if (UnmanagedObjects.Any())
|
||||
{
|
||||
var nowTime = DateTime.UtcNow;
|
||||
if ((LastGCCollectTime == null || (nowTime - LastGCCollectTime.Value).TotalSeconds > GC_COLLECT_INTERVAL_SECONDS))
|
||||
{
|
||||
LastGCCollectTime = nowTime;
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
}
|
||||
|
||||
UnmanagedObjects.Clear();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取版本号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetVersion()
|
||||
{
|
||||
{
|
||||
Version version = Assembly.GetEntryAssembly().GetName().Version;
|
||||
return version.ToString();
|
||||
}
|
||||
@ -145,7 +204,7 @@ namespace HT.Cloud.Code
|
||||
{
|
||||
int second = 365 * 24 * 60 * 60;
|
||||
context.Context.Response.Headers.Add("Cache-Control", new[] { "public,max-age=" + second });
|
||||
context.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
|
||||
}
|
||||
context.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user