ZR20090193-陈敬勇 10 months ago
commit e26145de59

@ -14,6 +14,12 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ds.Module.AppStartup\Ds.Module.AppStartup.csproj" />
<ProjectReference Include="..\Ds.Module.MediatR\Ds.Module.MediatR.csproj" />
<ProjectReference Include="..\DS.Module.Nuget\DS.Module.Nuget.csproj" />
<ProjectReference Include="..\DS.Module.SqlSugar\DS.Module.SqlSugar.csproj" />
<ProjectReference Include="..\Ds.Modules.DsEntity\Ds.Modules.DsEntity.csproj" />
<ProjectReference Include="..\Ds.WMS.Finance.MediatR\Ds.WMS.Finance.MediatR.csproj" />
<ProjectReference Include="..\Ds.WMS.WebCore\Ds.WMS.WebCore.csproj" />
</ItemGroup>

@ -7,7 +7,7 @@ var builder = WebApplication.CreateBuilder(args).UseMyConfiguration();
// 使用动态启动类 必须集成 webcore 即可实现配置文件的介绍
builder.UseDynamicStartups();
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
@ -27,6 +27,7 @@ builder.Services.AddSwaggerGen(c =>
c.IncludeXmlComments(xmlPath);
});
builder.UseDynamicStartups();
var app = builder.Build();

@ -3,7 +3,7 @@ namespace Ds.Module.AppStartup
{
public interface IDynamicStartup
{
void ConfigureServices(WebApplicationBuilder builder);
void Configure(WebApplication app);
public void ConfigureServices(WebApplicationBuilder builder);
public void Configure(WebApplication app);
}
}

@ -4,11 +4,19 @@ namespace Ds.WMS.WebCore
{
public class DsAppActionFilter : IActionFilter
{
/// <summary>
/// 执行后
/// </summary>
/// <param name="context"></param>
public void OnActionExecuted(ActionExecutedContext context)
{
}
/// <summary>
/// 执行前
/// </summary>
/// <param name="context"></param>
public void OnActionExecuting(ActionExecutingContext context)
{

@ -2,52 +2,58 @@
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
// 此方案可能查找不到程序集 顾使用手动加载方式
// var dynamicStartups = AppDomain.CurrentDomain.GetAssemblies()
//.SelectMany(a => a.GetTypes())
//.Select(t => new
//.Select(t =>
//new
//{
// Type = t,
// Attribute = t.GetCustomAttribute<DsStartupAttribute>()
//})
//.Where(x => x.Attribute != null)
//.OrderBy(x => x.Attribute.Order)
// .OrderBy(x => x.Attribute.Order)
//.Select(x => Activator.CreateInstance(x.Type))
//.OfType<IDynamicStartup>();
var assemblyNames = new List<string>
{
"Ds.WMS.Finance.MediatR",
"DS.Module.SqlSugar"
// 其他程序集的名称...
};
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);
@ -73,8 +79,4 @@ namespace Ds.WMS.WebCore
return app;
}
}
}
}

@ -1,6 +1,4 @@

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
namespace Ds.WMS.WebCore

Loading…
Cancel
Save