支持表自动迁移 & 优化ServiceProvider获取 & 包版本升级
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Infrastructure
|
||||
/// <summary>
|
||||
/// 服务提供器
|
||||
/// </summary>
|
||||
public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.InternalServices.BuildServiceProvider();
|
||||
public static IServiceProvider ServiceProvider => HttpContext?.RequestServices ?? InternalApp.ServiceProvider;
|
||||
/// <summary>
|
||||
/// 获取请求上下文
|
||||
/// </summary>
|
||||
|
||||
36
Infrastructure/Helper/AssemblyUtils.cs
Normal file
36
Infrastructure/Helper/AssemblyUtils.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure.Helper
|
||||
{
|
||||
public static class AssemblyUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取应用中的所有程序集
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Assembly> GetAssemblies()
|
||||
{
|
||||
var compilationLibrary = DependencyContext.Default
|
||||
.CompileLibraries
|
||||
.Where(x => !x.Serviceable && x.Type == "project")
|
||||
.ToList();
|
||||
return compilationLibrary.Select(p => Assembly.Load(new AssemblyName(p.Name)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取应用中的所有Type
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetAllTypes()
|
||||
{
|
||||
var assemblies = GetAssemblies();
|
||||
return assemblies.SelectMany(p => p.GetTypes());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AspectCore.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
|
||||
namespace Infrastructure
|
||||
{
|
||||
@@ -9,12 +10,12 @@ namespace Infrastructure
|
||||
/// <summary>
|
||||
/// 应用服务
|
||||
/// </summary>
|
||||
public static IServiceCollection InternalServices;
|
||||
public static IServiceProvider ServiceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// 全局配置构建器
|
||||
/// </summary>
|
||||
public static IConfigurationBuilder ConfigurationBuilder;
|
||||
//public static IConfigurationBuilder ConfigurationBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// 获取Web主机环境
|
||||
@@ -24,6 +25,6 @@ namespace Infrastructure
|
||||
/// <summary>
|
||||
/// 获取泛型主机环境
|
||||
/// </summary>
|
||||
public static IHostEnvironment HostEnvironment;
|
||||
//public static IHostEnvironment HostEnvironment;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Infrastructure.Startups
|
||||
{
|
||||
/// <summary>
|
||||
/// Program.cs里面的HostBuilder扩展
|
||||
/// </summary>
|
||||
public static class HostBuilderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Web 主机注入
|
||||
/// </summary>
|
||||
/// <param name="hostBuilder">Web主机构建器</param>
|
||||
/// <param name="assemblyName">外部程序集名称,如果HostingStartup存在多个程序集中可以使用;分隔,比如HostStartupLib;HostStartupLib2</param>
|
||||
/// <returns>IWebHostBuilder</returns>
|
||||
public static IWebHostBuilder Init(this IWebHostBuilder hostBuilder, string assemblyName)
|
||||
{
|
||||
hostBuilder.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, assemblyName);
|
||||
return hostBuilder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化程序扩展
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
public static void UseAppStartup(this IWebHostBuilder hostBuilder)
|
||||
{
|
||||
// 自动装载配置
|
||||
hostBuilder.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
// 存储环境对象
|
||||
InternalApp.HostEnvironment = (IHostEnvironment)hostingContext.HostingEnvironment;
|
||||
|
||||
// 加载配置
|
||||
//InternalApp.AddConfigureFiles(config, InternalApp.WebHostEnvironment);
|
||||
});
|
||||
// 自动注入 AddApp() 服务
|
||||
hostBuilder.ConfigureServices((services) =>
|
||||
{
|
||||
// 注册 Startup 过滤器
|
||||
//services.AddTransient<IStartupFilter, StartupFilter>();
|
||||
|
||||
// 添加全局配置和存储服务提供器
|
||||
InternalApp.InternalServices = services;
|
||||
|
||||
// 初始化应用服务
|
||||
//services.AddApp();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
using Infrastructure.Startups;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
//通过HostingStartup指定要启动的类型
|
||||
[assembly: HostingStartup(typeof(HostingStartup))]
|
||||
|
||||
namespace Infrastructure.Startups
|
||||
{
|
||||
public class HostingStartup : IHostingStartup
|
||||
{
|
||||
public void Configure(IWebHostBuilder builder)
|
||||
{
|
||||
// 自动装载配置
|
||||
builder.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
// 存储环境对象
|
||||
//InternalApp.HostEnvironment = hostingContext.HostingEnvironment;
|
||||
//InternalApp.HostEnvironment = (Microsoft.Extensions.Hosting.IHostEnvironment)hostingContext.HostingEnvironment;
|
||||
// 加载配置
|
||||
//InternalApp.AddConfigureFiles(config, InternalApp.WebHostEnvironment);
|
||||
InternalApp.ConfigurationBuilder = config;
|
||||
});
|
||||
|
||||
// 自动注入 AddApp() 服务
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
// 注册 Startup 过滤器
|
||||
//services.AddTransient<IStartupFilter, StartupFilter>();
|
||||
|
||||
// 添加全局配置和存储服务提供器
|
||||
InternalApp.InternalServices = services;
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user