升级.net8

This commit is contained in:
dell
2024-11-14 09:01:55 +08:00
parent 48e9e27fa0
commit abf72c3d58
86 changed files with 14229 additions and 10612 deletions

View File

@ -212,16 +212,33 @@ namespace HT.Cloud.Code
/// <returns></returns>
public static List<string> GetCacheKeys()
{
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache);
var cacheItems = entries as IDictionary;
var keys = new List<string>();
if (cacheItems == null) return keys;
foreach (DictionaryEntry cacheItem in cacheItems)
{
keys.Add(cacheItem.Key.ToString());
}
return keys;
}
}
#if NET8_0
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var entries = Cache.GetType().GetField("_coherentState", flags)?.GetValue(Cache);
var cacheItems = entries?.GetType()?.GetProperty("EntriesCollection", flags)?.GetValue(entries) as ICollection; //entries as IDictionary;
var keys = new List<string>();
if (cacheItems == null) return keys;
foreach (var item in cacheItems)
{
var methodInfo = item.GetType().GetProperty("Key");
var val = methodInfo.GetValue(item);
keys.Add(val.ToString());
}
return keys;
#else
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache);
var cacheItems = entries as IDictionary;
var keys = new List<string>();
if (cacheItems == null) return keys;
foreach (DictionaryEntry cacheItem in cacheItems)
{
keys.Add(cacheItem.Key.ToString());
}
return keys;
#endif
}
}
}

View File

@ -9,6 +9,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Serenity.Abstractions;
using System;
using System.Collections.Generic;
using System.IO;
@ -23,8 +24,8 @@ namespace HT.Cloud.Code
{
public class DefaultStartUp
{
protected IConfiguration Configuration { get; }
protected IWebHostEnvironment WebHostEnvironment { get; set; }
protected IConfiguration Configuration { get; set; }
protected IWebHostEnvironment WebHostEnvironment { get; set; }
public DefaultStartUp(IConfiguration configuration, IWebHostEnvironment env)
{
@ -37,7 +38,14 @@ namespace HT.Cloud.Code
public virtual void ConfigureServices(IServiceCollection services)
{
GlobalContext.SystemConfig = Configuration.GetSection("SystemConfig").Get<SystemConfig>();
GlobalContext.Services = services;
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
//没有设置环境变量就默认生产环境
if (string.IsNullOrWhiteSpace(environment))
environment = "Production";
Configuration = new ConfigurationBuilder().AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true).Build();
GlobalContext.SystemConfig = Configuration.GetSection("SystemConfig").Get<SystemConfig>();
GlobalContext.Services = services;
GlobalContext.Configuration = Configuration;
services.Configure<CookiePolicyOptions>(options =>
{
@ -135,9 +143,10 @@ namespace HT.Cloud.Code
public virtual void Configure(IApplicationBuilder app)
{
//实时通讯跨域
app.UseCors("CorsPolicy");
if (WebHostEnvironment.IsDevelopment())
GlobalContext.RootServices = app.ApplicationServices;
//实时通讯跨域
app.UseCors("CorsPolicy");
if (WebHostEnvironment.IsDevelopment())
{
GlobalContext.SystemConfig.Debug = true;
app.UseDeveloperExceptionPage();
@ -155,16 +164,18 @@ namespace HT.Cloud.Code
});
//启用 Gzip 和 Brotil 压缩功能
app.UseResponseCompression();
app.Use(next => context =>
{
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
return next(context);
});
// 执行下一个中间件
await next.Invoke();
// 释放所有未托管的服务提供器
GlobalContext.DisposeUnmanagedObjects();
});
//session
app.UseSession();
//路径
app.UseRouting();
GlobalContext.RootServices = app.ApplicationServices;
}
}
@ -312,54 +323,24 @@ namespace HT.Cloud.Code
public static IServiceCollection AddWorkerService(
this IServiceCollection @this)
{
var ret = new List<Type>();
try
{
var listAssemblies = new List<Assembly>();
var assemblies1 = Directory.GetFiles(AppContext.BaseDirectory, "*.dll");
foreach( var assembly in assemblies1)
{
Console.WriteLine(assembly);
}
var assemblies = Directory.GetFiles(AppContext.BaseDirectory, "*.dll")
.Select(x => x.Substring(@"\").Substring(@"/").Replace(".dll", ""));
//.Select(x => Assembly.Load(x)).ToArray();
foreach (var assembliesFile in assemblies)
{
try
{
Console.WriteLine(assembliesFile+"\n");
listAssemblies.Add(Assembly.Load(assembliesFile));
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
var arrAssemblies = listAssemblies.ToArray();
//排除列表
var ignoreList = new List<string> { "EventBusHostedService" };
foreach (var item in arrAssemblies)
{
ret.AddRange(item.GetTypes() //获取当前类库下所有类型
.Where(t => typeof(BackgroundService).IsAssignableFrom(t)) //获取间接或直接继承t的所有类型
.Where(t => !t.IsAbstract && t.IsClass && !ignoreList.Contains(t.Name)));//获取非抽象类 排除接口继承
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
foreach (var item in ret)
{
@this.AddTransient(typeof(IHostedService), item);
}
return @this;
}
var ret = new List<Type>();
var assemblies = Directory.GetFiles(AppContext.BaseDirectory, "HT.Cloud.*.dll")
.Select(x => x.Substring(@"\").Substring(@"/").Replace(".dll", ""))
.Select(x => Assembly.Load(x)).ToArray();
//排除列表
var ignoreList = new List<string> { "EventBusHostedService" };
foreach (var item in assemblies)
{
ret.AddRange(item.GetTypes() //获取当前类库下所有类型
.Where(t => typeof(BackgroundService).IsAssignableFrom(t)) //获取间接或直接继承t的所有类型
.Where(t => !t.IsAbstract && t.IsClass && !ignoreList.Contains(t.Name)));//获取非抽象类 排除接口继承
}
foreach (var item in ret)
{
@this.AddTransient(typeof(IHostedService), item);
}
return @this;
}
#endregion AddWorkerService

View File

@ -36,8 +36,11 @@
public const string SPECIAL_USER = "SPECIAL_USER"; //指定用户
public const string ALL_USER = "ALL_USER"; //所有用户
public const string SPECIAL_ROLE = "SPECIAL_ROLE"; //指定角色
public const string RUNTIME_SPECIAL_ROLE = "RUNTIME_SPECIAL_ROLE"; //运行时指定角色
public const string RUNTIME_SPECIAL_USER = "RUNTIME_SPECIAL_USER"; //运行时指定用户
public const string DEPARTMENT_MANAGER = "DEPARTMENT_MANAGER"; //部门负责人
public const string USER_MANAGER = "USER_MANAGER"; //直属上级
public const string MORE_USER_MANAGER = "MORE_USER_MANAGER"; //连续多级直属上级
public const string RUNTIME_SPECIAL_ROLE = "RUNTIME_SPECIAL_ROLE"; //运行时指定角色
public const string RUNTIME_SPECIAL_USER = "RUNTIME_SPECIAL_USER"; //运行时指定用户
/// <summary>
/// 节点执行权限类型
@ -94,13 +97,13 @@
public string[] roles { get; set; }
public string[] orgs { get; set; }
public bool currentDepart { get; set; }
}
}
/// <summary>
/// 节点执行结果标签
/// </summary>
public class Tag
{
/// <summary>
/// 节点执行结果标签
/// </summary>
public class Tag
{
/// <summary>
/// 1: 通过
/// 2不通过

View File

@ -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
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@ -35,22 +35,19 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="7.0.0" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="CSRedisCore" Version="3.8.670" />
<PackageReference Include="Autofac" Version="8.0.0" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="CSRedisCore" Version="3.8.803" />
<PackageReference Include="iTextSharp.LGPLv2.Core.Fix" Version="1.4.3" />
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serenity.Web" Version="3.14.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
<PackageReference Include="ZKWeb.System.Drawing" Version="4.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
</ItemGroup>
</Project>

View File

@ -32,7 +32,6 @@ namespace HT.Cloud.Code
public bool IsAdmin { get; set; }
public bool IsBoss { get; set; }
public bool IsSenior { get; set; }
public bool IsLeaderInDepts { get; set; }
public bool IsSaleman { get; set; }
// 拓展字段2019-03-03