using Ds.Module.AppStartup; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using System.Reflection; namespace Ds.WMS.WebCore.appstartup { /// /// /// public static class WebApplicationBuilderExtensions { public static WebApplicationBuilder UseDynamicStartups(this WebApplicationBuilder builder) { #region 此方案可能查找不到程序集 故使用手动加载方式 // 此方案可能查找不到程序集 顾使用手动加载方式 // 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(); #endregion 此方案可能查找不到程序集 故使用手动加载方式 var assemblyNames = new List { "Ds.WMS.Finance.MediatR", "DS.Module.SqlSugar", "Ds.WMS.MogonDb", "DS.Module.Swagger" // 其他程序集的名称... }; 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); } builder.Services.AddSingleton(dynamicStartups); return builder; } } public static class WebApplicationExtensions { public static WebApplication UseDynamicStartups(this WebApplication app) { var dynamicStartups = app.Services.GetRequiredService>(); foreach (var startup in dynamicStartups) { startup.Configure(app); } return app; } } }