diff --git a/Infrastructure/App/App.cs b/Infrastructure/App/App.cs index 28061116fe410f1dda1122706445d819b6d83e74..663e8efed42d625dcaf35cc78f8cefc641418ef3 100644 --- a/Infrastructure/App/App.cs +++ b/Infrastructure/App/App.cs @@ -10,7 +10,7 @@ namespace Infrastructure /// /// 服务提供器 /// - public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.InternalServices.BuildServiceProvider(); + public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.ServiceProvider; /// /// 获取请求上下文 /// diff --git a/Infrastructure/Helper/AssemblyUtils.cs b/Infrastructure/Helper/AssemblyUtils.cs new file mode 100644 index 0000000000000000000000000000000000000000..3934851e03d42ebc9de856b5ba412fd6063e28df --- /dev/null +++ b/Infrastructure/Helper/AssemblyUtils.cs @@ -0,0 +1,36 @@ +using Microsoft.Extensions.DependencyModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Infrastructure.Helper +{ + public static class AssemblyUtils + { + /// + /// 获取应用中的所有程序集 + /// + /// + public static IEnumerable GetAssemblies() + { + var compilationLibrary = DependencyContext.Default + .CompileLibraries + .Where(x => !x.Serviceable && x.Type == "project") + .ToList(); + return compilationLibrary.Select(p => Assembly.Load(new AssemblyName(p.Name))); + } + + /// + /// 获取应用中的所有Type + /// + /// + public static IEnumerable GetAllTypes() + { + var assemblies = GetAssemblies(); + return assemblies.SelectMany(p => p.GetTypes()); + } + } +} diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index a577cb443f715a4b2ba4e3b2d1b49188dd285dda..998c34b5b45499d5d2ec39dfce493724a7cbd4a3 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -11,6 +11,7 @@ + diff --git a/Infrastructure/InternalApp.cs b/Infrastructure/InternalApp.cs index f0d116e8a84cb57cd713dae5755b7ad7baeb371e..78e24d97a53e92a83eb46ca901c2eae1892361d1 100644 --- a/Infrastructure/InternalApp.cs +++ b/Infrastructure/InternalApp.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using System; namespace Infrastructure { @@ -9,12 +10,12 @@ namespace Infrastructure /// /// 应用服务 /// - public static IServiceCollection InternalServices; + public static IServiceProvider ServiceProvider; /// /// 全局配置构建器 /// - public static IConfigurationBuilder ConfigurationBuilder; + //public static IConfigurationBuilder ConfigurationBuilder; /// /// 获取Web主机环境 @@ -24,6 +25,6 @@ namespace Infrastructure /// /// 获取泛型主机环境 /// - public static IHostEnvironment HostEnvironment; + //public static IHostEnvironment HostEnvironment; } } diff --git a/Infrastructure/Startups/HostBuilderExtensions.cs b/Infrastructure/Startups/HostBuilderExtensions.cs deleted file mode 100644 index d183ca22b431c1b568f3119bd78d1df7fa86556e..0000000000000000000000000000000000000000 --- a/Infrastructure/Startups/HostBuilderExtensions.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Infrastructure.Startups -{ - /// - /// Program.cs里面的HostBuilder扩展 - /// - public static class HostBuilderExtensions - { - /// - /// Web 主机注入 - /// - /// Web主机构建器 - /// 外部程序集名称,如果HostingStartup存在多个程序集中可以使用;分隔,比如HostStartupLib;HostStartupLib2 - /// IWebHostBuilder - public static IWebHostBuilder Init(this IWebHostBuilder hostBuilder, string assemblyName) - { - hostBuilder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, assemblyName); - return hostBuilder; - } - - /// - /// 初始化程序扩展 - /// - /// - public static void UseAppStartup(this IWebHostBuilder hostBuilder) - { - // 自动装载配置 - hostBuilder.ConfigureAppConfiguration((hostingContext, config) => - { - // 存储环境对象 - InternalApp.HostEnvironment = (IHostEnvironment)hostingContext.HostingEnvironment; - - // 加载配置 - //InternalApp.AddConfigureFiles(config, InternalApp.WebHostEnvironment); - }); - // 自动注入 AddApp() 服务 - hostBuilder.ConfigureServices((services) => - { - // 注册 Startup 过滤器 - //services.AddTransient(); - - // 添加全局配置和存储服务提供器 - InternalApp.InternalServices = services; - - // 初始化应用服务 - //services.AddApp(); - }); - } - } -} diff --git a/Infrastructure/Startups/HostingStartup.cs b/Infrastructure/Startups/HostingStartup.cs deleted file mode 100644 index 076543da7bf285f2da000c35a1f7a1f8bd3915a8..0000000000000000000000000000000000000000 --- a/Infrastructure/Startups/HostingStartup.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Infrastructure.Startups; -using Microsoft.AspNetCore.Hosting; - -//通过HostingStartup指定要启动的类型 -[assembly: HostingStartup(typeof(HostingStartup))] - -namespace Infrastructure.Startups -{ - public class HostingStartup : IHostingStartup - { - public void Configure(IWebHostBuilder builder) - { - // 自动装载配置 - builder.ConfigureAppConfiguration((hostingContext, config) => - { - // 存储环境对象 - //InternalApp.HostEnvironment = hostingContext.HostingEnvironment; - //InternalApp.HostEnvironment = (Microsoft.Extensions.Hosting.IHostEnvironment)hostingContext.HostingEnvironment; - // 加载配置 - //InternalApp.AddConfigureFiles(config, InternalApp.WebHostEnvironment); - InternalApp.ConfigurationBuilder = config; - }); - - // 自动注入 AddApp() 服务 - builder.ConfigureServices(services => - { - // 注册 Startup 过滤器 - //services.AddTransient(); - - // 添加全局配置和存储服务提供器 - InternalApp.InternalServices = services; - }); - - } - } -} diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 8c1eeaf6150b05f6d22dae598a1bf0b9911734a1..cbf3934e3881adf7a7ee706b377a1739fc7b485f 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -1,10 +1,12 @@ using Infrastructure; +using Infrastructure.Helper; using Microsoft.Extensions.Configuration; using SqlSugar; using SqlSugar.IOC; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using ZR.Admin.WebApi.Framework; using ZR.Model.System; @@ -24,6 +26,9 @@ namespace ZR.Admin.WebApi.Extensions //仅本人数据权限 public static string DATA_SCOPE_SELF = "5"; + + + public static void AddDb(IConfiguration Configuration) { string connStr = Configuration.GetConnectionString("conn_db"); @@ -83,6 +88,18 @@ namespace ZR.Admin.WebApi.Extensions }; #endregion }); + + } + + public static void InitDb(this IServiceProvider service) + { + var db = DbScoped.SugarScope; + db.DbMaintenance.CreateDatabase(); + //db.CodeFirst. + var baseType = typeof(SysBase); + + var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && /*p.IsAssignableTo(baseType) && */p.GetCustomAttribute()!=null).ToArray(); + db.CodeFirst.SetStringDefaultLength(512).InitTables(entityes); } /// diff --git a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs index 94cf9f09fb6551b57f1b26ab3ebea8ad9de83446..6df2ab2ef966c6853af0ec645f301b85c0c41a37 100644 --- a/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs +++ b/ZR.Admin.WebApi/Extensions/SwaggerExtension.cs @@ -39,7 +39,7 @@ namespace ZR.Admin.WebApi.Extensions public static void AddSwaggerConfig(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); - IWebHostEnvironment hostEnvironment = App.GetRequiredService(); + //IWebHostEnvironment hostEnvironment = App.GetRequiredService(); services.AddSwaggerGen(c => { @@ -52,7 +52,7 @@ namespace ZR.Admin.WebApi.Extensions try { //添加文档注释 - c.IncludeXmlComments(Path.Combine(hostEnvironment.ContentRootPath, "ZRAdmin.xml"), true); + c.IncludeXmlComments("ZRAdmin.xml", true); } catch (Exception ex) { diff --git a/ZR.Admin.WebApi/Extensions/TasksExtension.cs b/ZR.Admin.WebApi/Extensions/TasksExtension.cs index 018a19c115b21f214d49b0a8f37d6090b152a6f5..62b43879dcfcf5e61868f52612fad9c6f52df2a3 100644 --- a/ZR.Admin.WebApi/Extensions/TasksExtension.cs +++ b/ZR.Admin.WebApi/Extensions/TasksExtension.cs @@ -35,7 +35,7 @@ namespace ZR.Admin.WebApi.Extensions { //var _tasksQzService = (ISysTasksQzService)App.GetRequiredService(typeof(ISysTasksQzService)); - ITaskSchedulerServer _schedulerServer = App.GetRequiredService(); + ITaskSchedulerServer _schedulerServer = app.ApplicationServices.GetRequiredService(); //var tasks = _tasksQzService.GetList(m => m.IsStart); var tasks = SqlSugar.IOC.DbScoped.SugarScope.Queryable().Where(m => m.IsStart).ToList(); diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 7eba8ac94401e2d259afdd9b3c8f53b136aa4d24..bd518f9414629444a68b9bd0f0a9d02cb27cc202 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -55,7 +55,7 @@ builder.Services.AddAuthentication(options => o.TokenValidationParameters = JwtUtil.ValidParameters(); }); -InternalApp.InternalServices = builder.Services; +//InternalApp.InternalServices = builder.Services; builder.Services.AddAppService(); builder.Services.AddSingleton(new AppSettings(builder.Configuration)); //开启计划任务 @@ -81,7 +81,8 @@ builder.Services.AddMvc(options => builder.Services.AddSwaggerConfig(); var app = builder.Build(); - +InternalApp.ServiceProvider = app.Services; +app.Services.InitDb(); app.UseSwagger(); //使可以多次多去body内容 diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index 8908b6f59bea48d5202facdbc06883ad80717ec5..5e212361c781209179a44043091871dc827604ba 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -22,15 +22,15 @@ - + - + - - + + - + diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj index bce93a2a27ddb701f1ef33618685327454aab062..47342954baafffea3fd5f5e1bb19ea9c0ebcc8d0 100644 --- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj +++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj @@ -12,6 +12,6 @@ - + diff --git a/ZR.Common/ZR.Common.csproj b/ZR.Common/ZR.Common.csproj index d376c8c3a0365faa435eea0909c545d4dc54d3cc..c987ab6c471f987e312c535e927015c714df4b6f 100644 --- a/ZR.Common/ZR.Common.csproj +++ b/ZR.Common/ZR.Common.csproj @@ -6,9 +6,9 @@ - - - + + + diff --git a/ZR.Model/System/Generate/GenTable.cs b/ZR.Model/System/Generate/GenTable.cs index 482ff3efaf5dea89e62b26f7d6dff70c666dd0ed..ab012da7a9ac18eb8fc4b89e4efd3fef3fd5dd1b 100644 --- a/ZR.Model/System/Generate/GenTable.cs +++ b/ZR.Model/System/Generate/GenTable.cs @@ -72,7 +72,7 @@ namespace ZR.Model.System.Generate /// /// 其他生成选项 /// - [SqlSugar.SugarColumn(IsJson = true)] + [SqlSugar.SugarColumn(IsJson = true, ColumnDataType = "nvarchar(4000)")] public Options Options { get; set; } #region 表额外字段 diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index 7e96b51d89e59e3369cf935cadf46bd81f712f3b..4d1cae26dafc3daac67c2e4f11482e85e9837dc9 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -5,9 +5,9 @@ - + - + diff --git a/ZR.Repository/ZR.Repository.csproj b/ZR.Repository/ZR.Repository.csproj index d55b3784bef6a4b9d367913cf4399d7fae177432..7aaa791efe70d563930fc8297e3429dc0f178348 100644 --- a/ZR.Repository/ZR.Repository.csproj +++ b/ZR.Repository/ZR.Repository.csproj @@ -10,11 +10,11 @@ - + - + diff --git a/ZR.Tasks/ZR.Tasks.csproj b/ZR.Tasks/ZR.Tasks.csproj index d5495fe24dbcd0875cd53ff8adf41e8ed38697ae..e1ad4084483d7173831cb314d9c47a9b2ca5edce 100644 --- a/ZR.Tasks/ZR.Tasks.csproj +++ b/ZR.Tasks/ZR.Tasks.csproj @@ -6,7 +6,7 @@ - +