ZR20090193-陈敬勇 10 months ago
commit e26145de59

@ -14,6 +14,12 @@
</ItemGroup> </ItemGroup>
<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" /> <ProjectReference Include="..\Ds.WMS.WebCore\Ds.WMS.WebCore.csproj" />
</ItemGroup> </ItemGroup>

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

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

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

@ -2,7 +2,6 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System.Reflection; using System.Reflection;
using System.Runtime.Loader;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore
{ {
@ -13,10 +12,34 @@ namespace Ds.WMS.WebCore
{ {
public static WebApplicationBuilder UseDynamicStartups(this WebApplicationBuilder builder) 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 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 dynamicStartups = AppDomain.CurrentDomain.GetAssemblies() var startups = assembly.GetTypes()
.SelectMany(a => a.GetTypes())
.Select(t => .Select(t =>
new new
{ {
@ -26,28 +49,11 @@ namespace Ds.WMS.WebCore
.Where(x => x.Attribute != null) .Where(x => x.Attribute != null)
.OrderBy(x => x.Attribute.Order) .OrderBy(x => x.Attribute.Order)
.Select(x => Activator.CreateInstance(x.Type)) .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>() .OfType<IDynamicStartup>()
.ToList(); .ToList();
// var dynamicStartups = AssemblyLoadContext.Default.Assemblies
//.SelectMany(a => a.GetTypes()) dynamicStartups.AddRange(startups);
//.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) foreach (var startup in dynamicStartups)
{ {
startup.ConfigureServices(builder); startup.ConfigureServices(builder);
@ -73,8 +79,4 @@ namespace Ds.WMS.WebCore
return app; return app;
} }
} }
} }

@ -1,6 +1,4 @@
 using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
namespace Ds.WMS.WebCore namespace Ds.WMS.WebCore

Loading…
Cancel
Save