From 65543e651cfe4b144038a466251335d92245d08a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Thu, 1 Jun 2023 18:47:11 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/AppSettings.cs | 26 ++++++++++++++++--- .../Extensions/AppServiceExtensions.cs | 9 +++++-- ZR.Admin.WebApi/Program.cs | 2 +- ZR.Admin.WebApi/appsettings.json | 3 ++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Infrastructure/AppSettings.cs b/Infrastructure/AppSettings.cs index f2a6d2b..f93c764 100644 --- a/Infrastructure/AppSettings.cs +++ b/Infrastructure/AppSettings.cs @@ -45,17 +45,26 @@ namespace Infrastructure public static List App(params string[] sections) { List list = new(); - // 引用 Microsoft.Extensions.Configuration.Binder 包 - Configuration.Bind(string.Join(":", sections), list); + try + { + if (Configuration != null && sections.Any()) + { + Configuration.Bind(string.Join(":", sections), list); + } + } + catch + { + return list; + } return list; } public static T Bind(string key, T t) { Configuration.Bind(key, t); - return t; } + public static T GetAppConfig(string key, T defaultValue = default) { T setting = (T)Convert.ChangeType(Configuration[key], typeof(T)); @@ -74,5 +83,16 @@ namespace Infrastructure { return Configuration[key]; } + + /// + /// 获取配置节点并转换成指定类型 + /// + /// 节点类型 + /// 节点路径 + /// 节点类型实例 + public static T Get(string key) + { + return Configuration.GetSection(key).Get(); + } } } diff --git a/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs b/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs index f888f1f..03b9105 100644 --- a/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs +++ b/ZR.Admin.WebApi/Extensions/AppServiceExtensions.cs @@ -1,4 +1,5 @@ -using Infrastructure.Attribute; +using Infrastructure; +using Infrastructure.Attribute; using Microsoft.Extensions.DependencyInjection; using System.Linq; using System.Reflection; @@ -16,7 +17,11 @@ namespace ZR.Admin.WebApi.Extensions /// public static void AddAppService(this IServiceCollection services) { - string[] cls = new string[] { "ZR.Repository", "ZR.Service", "ZR.Tasks" }; + var cls = AppSettings.Get("InjectClass"); + if (cls == null || cls.Length <= 0) + { + throw new Exception("请更新appsettings类"); + } foreach (var item in cls) { Register(services, item); diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index d217236..e984fb4 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -61,8 +61,8 @@ builder.Services.AddAuthentication(options => }); //InternalApp.InternalServices = builder.Services; -builder.Services.AddAppService(); builder.Services.AddSingleton(new AppSettings(builder.Configuration)); +builder.Services.AddAppService(); //开启计划任务 builder.Services.AddTaskSchedulers(); //初始化db diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 223bdf8..ee9155d 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -18,7 +18,8 @@ "SecretKey": "SecretKey-ZRADMIN.NET-20210101", "Expire": 1440 //jwt登录过期时间(分) }, - "InitDb": false,//是否初始化db + "InjectClass": [ "ZR.Repository", "ZR.Service", "ZR.Tasks" ],//自动注入类 + "InitDb": false, //是否初始化db "DemoMode": false, //是否演示模式 "Upload": { "uploadUrl": "http://localhost:8888", //本地存储资源访问路径