using Ds.Module.AppStartup; 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 assembly = Assembly.Load("DS.WMS.FinanceApi"); var types = assembly.GetTypes() .Where(t => t.GetCustomAttributes().Any()) .ToList(); var dynamictups = AssemblyLoadContext.Default.Assemblies .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().ToList(); 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(); 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; } } }