You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ds8-solution-pro/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtens...

77 lines
2.0 KiB
C#

11 months ago
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<DsStartupAttribute>().Any())
.ToList();
var dynamictups = AssemblyLoadContext.Default.Assemblies
.SelectMany(a => a.GetTypes())
.Select(t => new
{
Type = t,
Attribute = t.GetCustomAttribute<DsStartupAttribute>()
})
.Where(x => x.Attribute != null)
.OrderBy(x => x.Attribute.Order)
.Select(x => Activator.CreateInstance(x.Type))
.OfType<IDynamicStartup>().ToList();
var dynamicStartups = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Select(t => new
{
Type = t,
Attribute = t.GetCustomAttribute<DsStartupAttribute>()
})
.Where(x => x.Attribute != null)
.OrderBy(x => x.Attribute.Order)
.Select(x => Activator.CreateInstance(x.Type))
.OfType<IDynamicStartup>();
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<IEnumerable<IDynamicStartup>>();
foreach (var startup in dynamicStartups)
{
startup.Configure(app);
}
return app;
}
}
}