升级.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

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