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...

81 lines
2.2 KiB
C#

using Ds.Module.AppStartup;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Runtime.Loader;
namespace Ds.WMS.WebCore
{
/// <summary>
///
/// </summary>
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<DsStartupAttribute>()
})
.Where(x => x.Attribute != null)
.OrderBy(x => x.Attribute.Order)
.Select(x => Activator.CreateInstance(x.Type))
.OfType<IDynamicStartup>();
var list = AssemblyLoadContext.Default.Assemblies.SelectMany(a => a.GetTypes())
.Select(t => new
{
Type = t,
Attribute = t.GetCustomAttribute<DsStartupAttribute>()
})
// .Where(x => x.Attribute != null)
.OfType<IDynamicStartup>()
.ToList();
// var dynamicStartups = 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>();
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;
}
}
}