From 14ee93ab2b1d87d7f736f7b2ff2bc386282a838e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=AC=A3?= <83699462@qq.com> Date: Tue, 16 Jan 2024 14:29:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9F=A5=E6=89=BE=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E7=A8=8B=E5=BA=8F=E9=9B=86=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DS.WMS.FinanceApi.csproj | 6 ++ ds-wms-service/DS.WMS.FinanceApi/Program.cs | 3 +- .../Ds.Module.AppStartup/IDynamicStartup.cs | 4 +- .../Ds.WMS.WebCore/DsAppActionFilter.cs | 8 +++ .../WebApplicationBuilderExtensions.cs | 70 ++++++++++--------- .../WebApplicationFactoryExtensions.cs | 4 +- 6 files changed, 55 insertions(+), 40 deletions(-) diff --git a/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj b/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj index e9d271a6..a00fb181 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj +++ b/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj @@ -14,6 +14,12 @@ + + + + + + diff --git a/ds-wms-service/DS.WMS.FinanceApi/Program.cs b/ds-wms-service/DS.WMS.FinanceApi/Program.cs index 51d42ae0..28725f99 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/Program.cs +++ b/ds-wms-service/DS.WMS.FinanceApi/Program.cs @@ -7,7 +7,7 @@ var builder = WebApplication.CreateBuilder(args).UseMyConfiguration(); // 使用动态启动类 必须集成 webcore 即可实现配置文件的介绍 -builder.UseDynamicStartups(); + // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -27,6 +27,7 @@ builder.Services.AddSwaggerGen(c => c.IncludeXmlComments(xmlPath); }); +builder.UseDynamicStartups(); var app = builder.Build(); diff --git a/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs b/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs index 2db8f867..7b32fa34 100644 --- a/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs +++ b/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs @@ -3,7 +3,7 @@ namespace Ds.Module.AppStartup { public interface IDynamicStartup { - void ConfigureServices(WebApplicationBuilder builder); - void Configure(WebApplication app); + public void ConfigureServices(WebApplicationBuilder builder); + public void Configure(WebApplication app); } } \ No newline at end of file diff --git a/ds-wms-service/Ds.WMS.WebCore/DsAppActionFilter.cs b/ds-wms-service/Ds.WMS.WebCore/DsAppActionFilter.cs index c972b6f6..eef8e32a 100644 --- a/ds-wms-service/Ds.WMS.WebCore/DsAppActionFilter.cs +++ b/ds-wms-service/Ds.WMS.WebCore/DsAppActionFilter.cs @@ -4,11 +4,19 @@ namespace Ds.WMS.WebCore { public class DsAppActionFilter : IActionFilter { + /// + /// 鎵ц鍚 + /// + /// public void OnActionExecuted(ActionExecutedContext context) { } + /// + /// 鎵ц鍓 + /// + /// public void OnActionExecuting(ActionExecutingContext context) { diff --git a/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs b/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs index 97e9c503..74c93950 100644 --- a/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs +++ b/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs @@ -2,52 +2,58 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using System.Reflection; -using System.Runtime.Loader; namespace Ds.WMS.WebCore { /// - /// + /// /// public static class WebApplicationBuilderExtensions { public static WebApplicationBuilder UseDynamicStartups(this WebApplicationBuilder builder) { - - - var dynamicStartups = AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(a => a.GetTypes()) - .Select(t => - new - { - Type = t, - Attribute = t.GetCustomAttribute() - }) - .Where(x => x.Attribute != null) - .OrderBy(x => x.Attribute.Order) - .Select(x => Activator.CreateInstance(x.Type)) - .OfType(); - - var list = AssemblyLoadContext.Default.Assemblies.SelectMany(a => a.GetTypes()) - .Select(t => new - { - Type = t, - Attribute = t.GetCustomAttribute() - }) - // .Where(x => x.Attribute != null) - .OfType() - .ToList(); - // var dynamicStartups = AssemblyLoadContext.Default.Assemblies + // 姝ゆ柟妗堝彲鑳芥煡鎵句笉鍒扮▼搴忛泦 椤句娇鐢ㄦ墜鍔ㄥ姞杞芥柟寮 + // var dynamicStartups = AppDomain.CurrentDomain.GetAssemblies() //.SelectMany(a => a.GetTypes()) - //.Select(t => new + //.Select(t => + //new //{ // Type = t, // Attribute = t.GetCustomAttribute() //}) //.Where(x => x.Attribute != null) - //.OrderBy(x => x.Attribute.Order) + // .OrderBy(x => x.Attribute.Order) //.Select(x => Activator.CreateInstance(x.Type)) //.OfType(); + + var assemblyNames = new List +{ + "Ds.WMS.Finance.MediatR", + "DS.Module.SqlSugar" + // 鍏朵粬绋嬪簭闆嗙殑鍚嶇О... +}; + + var dynamicStartups = new List(); + + foreach (var assemblyName in assemblyNames) + { + var assembly = Assembly.Load(assemblyName); + + var startups = assembly.GetTypes() + .Select(t => + new + { + Type = t, + Attribute = t.GetCustomAttribute() + }) + .Where(x => x.Attribute != null) + .OrderBy(x => x.Attribute.Order) + .Select(x => Activator.CreateInstance(x.Type)) + .OfType() + .ToList(); + + dynamicStartups.AddRange(startups); + } foreach (var startup in dynamicStartups) { startup.ConfigureServices(builder); @@ -73,8 +79,4 @@ namespace Ds.WMS.WebCore return app; } } - - -} - - +} \ No newline at end of file diff --git a/ds-wms-service/Ds.WMS.WebCore/WebApplicationFactoryExtensions.cs b/ds-wms-service/Ds.WMS.WebCore/WebApplicationFactoryExtensions.cs index edcadbe5..2c1685fd 100644 --- a/ds-wms-service/Ds.WMS.WebCore/WebApplicationFactoryExtensions.cs +++ b/ds-wms-service/Ds.WMS.WebCore/WebApplicationFactoryExtensions.cs @@ -1,6 +1,4 @@ -锘 - -using Microsoft.AspNetCore.Builder; +锘縰sing Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; namespace Ds.WMS.WebCore