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.
87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using Ds.Module.AppStartup;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Reflection;
|
|
|
|
namespace Ds.WMS.WebCore
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
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<DsStartupAttribute>()
|
|
//})
|
|
//.Where(x => x.Attribute != null)
|
|
// .OrderBy(x => x.Attribute.Order)
|
|
//.Select(x => Activator.CreateInstance(x.Type))
|
|
//.OfType<IDynamicStartup>();
|
|
|
|
#endregion 此方案可能查找不到程序集 故使用手动加载方式
|
|
|
|
var assemblyNames = new List<string>
|
|
{
|
|
"Ds.WMS.Finance.MediatR",
|
|
"DS.Module.SqlSugar",
|
|
"Ds.WMS.MogonDb"
|
|
// 其他程序集的名称...
|
|
};
|
|
|
|
var dynamicStartups = new List<IDynamicStartup>();
|
|
|
|
foreach (var assemblyName in assemblyNames)
|
|
{
|
|
var assembly = Assembly.Load(assemblyName);
|
|
|
|
var startups = assembly.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();
|
|
|
|
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<IEnumerable<IDynamicStartup>>();
|
|
|
|
foreach (var startup in dynamicStartups)
|
|
{
|
|
startup.Configure(app);
|
|
}
|
|
|
|
return app;
|
|
}
|
|
}
|
|
} |