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.
326 lines
11 KiB
C#
326 lines
11 KiB
C#
using Estsh.Core.Config;
|
|
using Estsh.Core.Dapper;
|
|
using Estsh.Core.Quartz.Extensions;
|
|
using Estsh.Core.Util;
|
|
|
|
using Estsh.Core.Web.Base.Filter;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyModel;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.Loader;
|
|
using static Org.BouncyCastle.Math.EC.ECCurve;
|
|
|
|
namespace Estsh.Core.Extensions
|
|
{
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
//private readonly ILogger<ServiceCollectionExtensions> logger;
|
|
|
|
/// <summary>
|
|
/// 添加自动扫描注入Service服务和Respository仓储
|
|
/// </summary>
|
|
/// <param name="services">服务集合</param>
|
|
/// <returns>服务集合</returns>
|
|
public static IServiceCollection AddAutoScanInjection(this IServiceCollection services)
|
|
{
|
|
var compilationLibrary = DependencyContext.Default
|
|
.CompileLibraries
|
|
.Where(x => x.Name.StartsWith("Estsh.Core"))
|
|
.ToList();
|
|
List<Assembly> assemblyList = new List<Assembly>();
|
|
|
|
foreach (var _compilation in compilationLibrary)
|
|
{
|
|
try
|
|
{
|
|
assemblyList.Add(AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(_compilation.Name)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(_compilation.Name + ex.Message);
|
|
}
|
|
}
|
|
|
|
assemblyList.ForEach(a =>
|
|
{
|
|
a.GetTypes().Where(t => typeof(IDependency).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract).ToList().ForEach(c =>
|
|
{
|
|
var serviceType = c.GetInterface($"I{c.Name}");
|
|
if (serviceType != null)
|
|
{
|
|
services.AddScoped(serviceType, c);
|
|
}
|
|
else
|
|
{
|
|
services.AddScoped(c);
|
|
}
|
|
});
|
|
});
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 主项目服务注册
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection MainStartup(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
#region 注册控制器和视图
|
|
services.AddControllersWithViews(a => a.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true)
|
|
.ConfigureApplicationPartManager(apm =>
|
|
{
|
|
// 加载插件项目
|
|
var targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
|
|
var pluginAssembly = Directory.EnumerateFiles(targetPath, "Estsh.Core.Web.Plugin*.dll")
|
|
.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath);
|
|
foreach (var item in pluginAssembly)
|
|
{
|
|
foreach (var part in new DefaultApplicationPartFactory().GetApplicationParts(item))
|
|
{
|
|
apm.ApplicationParts.Add(part);
|
|
}
|
|
foreach (var part in new CompiledRazorAssemblyApplicationPartFactory().GetApplicationParts(item))
|
|
{
|
|
apm.ApplicationParts.Add(part);
|
|
}
|
|
}
|
|
}
|
|
).AddNewtonsoftJson();
|
|
#endregion
|
|
|
|
#region 基础服务注册
|
|
services.BaseStartup(configuration);
|
|
#endregion
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插件项目服务注册
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection PluginStartup(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
#region 注册控制器和视图
|
|
services.AddControllersWithViews()
|
|
.ConfigureApplicationPartManager(apm =>
|
|
{
|
|
// 把主项目作为插件加载进来
|
|
var targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
|
|
var pluginAssembly = Directory.EnumerateFiles(targetPath, "Estsh.Core.Web.dll")
|
|
.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath);
|
|
foreach (var item in pluginAssembly)
|
|
{
|
|
foreach (var part in new DefaultApplicationPartFactory().GetApplicationParts(item))
|
|
{
|
|
apm.ApplicationParts.Add(part);
|
|
}
|
|
foreach (var part in new CompiledRazorAssemblyApplicationPartFactory().GetApplicationParts(item))
|
|
{
|
|
apm.ApplicationParts.Add(part);
|
|
}
|
|
}
|
|
}
|
|
).AddNewtonsoftJson();
|
|
#endregion
|
|
|
|
#region 基础服务注册
|
|
services.BaseStartup(configuration);
|
|
#endregion
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 基础服务注册
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection BaseStartup(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
#region 初始化全局配置
|
|
InitConfiguration(configuration);
|
|
#endregion
|
|
|
|
#region 皮肤筛选器的全局注册
|
|
services.AddControllers(o => o.Filters.Add(typeof(SkinResultFilter)));
|
|
|
|
#endregion
|
|
|
|
#region 注册Session
|
|
services.AddSession(options => {
|
|
options.IdleTimeout = TimeSpan.FromMinutes(GlobalConfig.SessionConfig.IdleTimeout);
|
|
});
|
|
#endregion
|
|
|
|
#region 注册Sweagger
|
|
services.AddSwaggerGen();
|
|
#endregion
|
|
|
|
#region 注册Dapper连接
|
|
//services.AddSingleton(dapper => new DapperDbContext(
|
|
// configuration.GetConnectionString("DbConnectionString")
|
|
//));
|
|
services.AddScoped(dapper => new DapperDbContext(
|
|
GlobalConfig.DbConfig.ConnectionString
|
|
//configuration.GetConnectionString("DbConnectionString")
|
|
));
|
|
#endregion
|
|
|
|
#region 自动注入Service和Repository处理
|
|
services.AddAutoScanInjection();
|
|
#endregion
|
|
|
|
#region 跨域处理
|
|
services.AddCors(policy =>
|
|
{
|
|
policy.AddPolicy("CorsPolicy", opt => opt
|
|
.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.WithExposedHeaders("Content-Disposition"));
|
|
});
|
|
#endregion
|
|
|
|
#region 支持ASP.NET CORE内存缓存组件
|
|
services.AddMemoryCache();
|
|
#endregion
|
|
|
|
#region 初始化软件授权注册信息
|
|
//LicenseUtil.GetLicenseRegistInfo();
|
|
//LicenseUtil.LicenseActivate(configuration.GetSection("LicenseConfig:LicenseString").Value);
|
|
#endregion
|
|
|
|
#region 身份认证授权
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; ;
|
|
|
|
});
|
|
#endregion
|
|
|
|
#region 任务调度处理
|
|
services.AddQuartzUI();
|
|
//services.AddQuartzUI(GlobalConfig.DbConfig.ConnectionString);
|
|
services.AddQuartzClassJobs("Estsh.Core");
|
|
#endregion
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 主项目app构建
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="env"></param>
|
|
/// <returns></returns>
|
|
public static IApplicationBuilder MainAppBuilder(this IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
app.BaseAppBuilder(env);
|
|
|
|
return app;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插件项目app构建
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="env"></param>
|
|
/// <returns></returns>
|
|
public static IApplicationBuilder PluginAppBuilder(this IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
app.BaseAppBuilder(env);
|
|
|
|
return app;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 基础app构建
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="env"></param>
|
|
/// <returns></returns>
|
|
public static IApplicationBuilder BaseAppBuilder(this IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
app.UseHsts();
|
|
|
|
}
|
|
app.UseCors("CorsPolicy");
|
|
//app.UseHttpsRedirection();
|
|
app.UseStaticFiles();
|
|
|
|
// 启用任务调度
|
|
app.UseQuartz();
|
|
|
|
//启用全局统一异常处理
|
|
app.UseMiddleware<GlobalExceptionHandler>();
|
|
|
|
app.UseRouting();
|
|
app.UseAuthorization();
|
|
app.UseAuthentication();
|
|
app.UseSession();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
endpoints.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
endpoints.MapControllerRoute(
|
|
name: "areas",
|
|
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
|
|
});
|
|
//app.UseStatusCodePages();
|
|
|
|
return app;
|
|
}
|
|
|
|
#region 初始化全局静态配置
|
|
|
|
/// <summary>
|
|
/// 初始化全局静态配置
|
|
/// </summary>
|
|
private static void InitConfiguration(IConfiguration configuration)
|
|
{
|
|
GlobalConfig.DbConfig = configuration.GetSection("DbConfig").Get<DbConfig>();
|
|
// 配置文件中获取皮肤的基础配置
|
|
GlobalConfig.SkinConfig = configuration.GetSection("SkinConfig").Get<SkinConfig>();
|
|
// 加载JWT配置
|
|
GlobalConfig.JwtConfig = configuration.GetSection("JwtConfig").Get<JwtConfig>();
|
|
// 加载JWT配置
|
|
GlobalConfig.SessionConfig = configuration.GetSection("SessionConfig").Get<SessionConfig>();
|
|
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|