ow
This commit is contained in:
1 parent
31fbe199c1
commit
1c0c83af5f
469 files changed
+62544
-5
No files matched your search
@@ -0,0 +1,136 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Options;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.Authorization;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using CloudBuilder.Core.Policy;
|
||||
using CloudBuilder.Core.Service;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
/*
|
||||
使用JWT:
|
||||
1.认证(AddAuthentication)
|
||||
1.1开启Bearer认证
|
||||
1.2注册JwtBearer服务
|
||||
3.开启中间件(app.UseAuthentication())
|
||||
4.实现IAuthenticationService接口认证服务
|
||||
5.实现IAuthorizationPermissionService接口授权服务
|
||||
|
||||
2.授权(AddAuthorization)
|
||||
2.1无状态授权[Authorize]
|
||||
1.2有状态授权[Authorize["Permission"]] AddAuthorization注册自定义授权
|
||||
|
||||
*/
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Authorization
|
||||
{
|
||||
[PackLevel(45)]
|
||||
public class AuthorizationPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Framework;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将服务添加到依赖注入服务容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
/*策略授权
|
||||
1.导入
|
||||
Microsoft.AspNetCore.Authorization
|
||||
Microsoft.AspNetCore.Authentication.JwtBearer
|
||||
2.具体实现逻辑交由外部的PermissionServices:IAuthorizationPermissionService来处理
|
||||
*/
|
||||
|
||||
CloudBuilderOptions section = builder.CloudBuilderOptions;
|
||||
if (section.Jwt == null) return services;
|
||||
|
||||
var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(section.Jwt.Secret)), SecurityAlgorithms.HmacSha256);
|
||||
|
||||
// 角色与接口的权限要求参数
|
||||
var permissionRequirement = new PermissionRequirement();
|
||||
|
||||
//初始化认证信息
|
||||
services.AddAuthentication(x =>
|
||||
{
|
||||
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(x =>
|
||||
{
|
||||
x.RequireHttpsMetadata = false;
|
||||
x.SaveToken = true;
|
||||
|
||||
//Token Validation Parameters
|
||||
x.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ClockSkew= TimeSpan.Zero,
|
||||
//通常,验证库(至少是MS之一)可以补偿时钟偏斜。 ClockSkew的默认值为5分钟。
|
||||
ValidateIssuerSigningKey = true,
|
||||
//获取或设置要使用的Microsoft.IdentityModel.Tokens.SecurityKey用于签名验证。
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(section.Jwt.Secret)),
|
||||
//获取或设置一个System.String,它表示将使用的有效发行者检查代币的发行者。
|
||||
ValidIssuer = section.Jwt.Issuer,
|
||||
//获取或设置一个字符串,该字符串表示将用于检查的有效受众反对令牌的观众。
|
||||
ValidAudience = section.Jwt.Audience,
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
NameClaimType = ClaimPolicy.CLAINM_USERID,
|
||||
RoleClaimType = ClaimPolicy.ROLE,
|
||||
};
|
||||
});
|
||||
|
||||
//启用全局授权(所有的Controller就无需加入Authorize属性,如果加入就会重复执行授权)
|
||||
if (section.EnableGlobalAuthorize)
|
||||
{
|
||||
services.Configure<MvcOptions>(options =>
|
||||
{
|
||||
options.Filters.Add(new AuthorizeFilter());
|
||||
});
|
||||
}
|
||||
|
||||
//初始化授权信息(使用自定义复杂的授权)
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy(AuthorizePolicy.PERMISSION, policy => policy.Requirements.Add(permissionRequirement));
|
||||
});
|
||||
|
||||
var baseType = typeof(IAuthorize);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.AddScoped(implementType.GetInterfaces().FirstOrDefault(), implementType);
|
||||
}
|
||||
|
||||
services.AddSingleton(new JwtGenerate(section.Jwt));
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Options;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.DatabaseAccessor;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
[PackLevel(15)]
|
||||
public class EntityFrameworkPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Framework;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将注册实体类服务添加到依赖注入服务容器中
|
||||
/// 各模块然后自动注入实体类(含多关键字和无关键字)
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
var baseType = typeof(IMasterRegistEntityType<ModelBuilder>);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && baseType.IsAssignableFrom(type))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.AddSingleton(baseType, implementType);
|
||||
}
|
||||
|
||||
services.TryAddScoped(typeof(IRepository<>), typeof(Repository<>));
|
||||
services.TryAddScoped(typeof(IUnitOfWork), typeof(UnitOfWork));
|
||||
|
||||
//当你直接在控制器方法上使用[UnitOfWork] 特性时,ASP.NET Core 的过滤器管道会自动实例化这个特性,不需要手动注册。
|
||||
//如果你的 UnitOfWorkAttribute 没有构造函数依赖,那么不需要注册:
|
||||
services.TryAddScoped<UnitOfWorkAttribute>();
|
||||
|
||||
services.TryAddKeyedScoped<DbContext, SecondaryDbContext>(SecondaryDbContext.SECONDARY_DB_CONTEXT);
|
||||
services.TryAddKeyedScoped<DbContext, MasterDbContext>(MasterDbContext.MASTER_DB_CONTEXT);
|
||||
|
||||
services.AddDbContext<SecondaryDbContext>(opts =>
|
||||
opts.UseSqlServer(builder.CloudBuilderOptions.GetDbContextOptions(SecondaryDbContext.SECONDARY_DB_CONTEXT).ConnectionString));
|
||||
|
||||
services.AddDbContext<MasterDbContext>(opts =>
|
||||
opts.UseSqlServer(builder.CloudBuilderOptions.GetDbContextOptions(MasterDbContext.MASTER_DB_CONTEXT).ConnectionString));
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
/// <summary>
|
||||
/// 每一个模块注册实体类
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public interface IMasterRegistEntityType<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类,调用AddEntityType和RegistKeys
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistEntity(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 如果Entity实体类不是单一主键,要单独重写注入(自动生成)
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistKeys(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="assemblyName"></param>
|
||||
void AddEntityType(T builder, string assemblyName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
/// <summary>
|
||||
/// 每一个模块注册从库实体类
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public interface ISecondaryRegistEntityType<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类,调用AddEntityType和RegistKeys
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistEntity(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 如果Entity实体类不是单一主键,要单独重写注入(自动生成)
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
void RegistKeys(T builder);
|
||||
|
||||
/// <summary>
|
||||
/// 注入所有Entity实体类
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
/// <param name="assemblyName"></param>
|
||||
void AddEntityType(T builder, string assemblyName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using CloudBuilder.Core.Authorization;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EFCore
|
||||
{
|
||||
public class RegistEntityHelper
|
||||
{
|
||||
public static void AddMasterEntityType(ModelBuilder builder, string _assemblyName)
|
||||
{
|
||||
var eType = typeof(IEntity<>);
|
||||
AddMasterEntityType(builder, _assemblyName, eType);
|
||||
}
|
||||
|
||||
public static void AddSecondaryEntityType(ModelBuilder builder, string _assemblyName)
|
||||
{
|
||||
var eType = typeof(ISecondary);
|
||||
AddMasterEntityType(builder, _assemblyName, eType);
|
||||
}
|
||||
|
||||
public static void AddMasterEntityType(ModelBuilder builder, string _assemblyName, Type eType)
|
||||
{
|
||||
var mType = typeof(IEntity<>);
|
||||
var sType = typeof(ISecondary);
|
||||
|
||||
var entityTypes = Assembly.Load(new AssemblyName(_assemblyName)).GetTypes()
|
||||
.Where(type => !string.IsNullOrWhiteSpace(type.Namespace))
|
||||
.Where(type => type.GetTypeInfo().IsClass)
|
||||
.Where(type => type.GetTypeInfo().BaseType != null)
|
||||
.Where(type => type.HasImplementedRawGeneric(eType)).ToList();
|
||||
|
||||
if (eType == mType) entityTypes = entityTypes.Where(type => !type.HasImplementedRawGeneric(sType)).ToList();
|
||||
|
||||
NotMappedAttribute notMapped = null;
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
//排除实体类标记[NotMapped]
|
||||
notMapped = entityType.GetCustomAttribute<NotMappedAttribute>();
|
||||
if (notMapped != null) continue;
|
||||
|
||||
if (builder.Model.FindEntityType(entityType) != null)
|
||||
continue;
|
||||
builder.Model.AddEntityType(entityType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using CloudBuilder.Core.DatabaseAccessor;
|
||||
using CloudBuilder.Core.DependencyInjection.EFCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using CloudBuilder.Core.Service;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EventBuses
|
||||
{
|
||||
[PackLevel(30)]
|
||||
public class EventBusesPack : CloudBuilderPack
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Business;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 3;
|
||||
|
||||
/// <summary>
|
||||
/// 将事件总线的发布者和监听者都添加到依赖注入容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
var baseType = typeof(IEventPublisher);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.AddScoped(implementType.GetInterfaces().FirstOrDefault(), implementType);
|
||||
}
|
||||
|
||||
baseType = typeof(IEventSubscriber);
|
||||
|
||||
types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.AddScoped(implementType.GetInterfaces().FirstOrDefault(), implementType);
|
||||
}
|
||||
/*
|
||||
* 取出所有监听服务
|
||||
IEnumerable<ISecurityUserRegisterSubscriber> subscribers = service.ServiceProvider.GetServices<ISecurityUserRegisterSubscriber>();
|
||||
*/
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EventBuses
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件数据,所有事件都要实现该接口
|
||||
/// </summary>
|
||||
public class EventData
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 事件编号
|
||||
/// </summary>
|
||||
public string? EventId { get; set; }
|
||||
/// <summary>
|
||||
/// 获取 事件发生的时间
|
||||
/// </summary>
|
||||
public DateTime EventTime { get; } = DateTime.Now;
|
||||
/// <summary>
|
||||
/// 获取或设置 事件源,触发事件的对象
|
||||
/// </summary>
|
||||
public object? EventSource { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EventBuses
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件发布者
|
||||
/// </summary>
|
||||
public interface IEventPublisher
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.EventBuses
|
||||
{
|
||||
/// <summary>
|
||||
/// 定义事件订阅者
|
||||
/// </summary>
|
||||
public interface IEventSubscriber
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using CloudBuilder.Core.DependencyInjection.Request;
|
||||
using CloudBuilder.Core.Policy;
|
||||
using DocumentFormat.OpenXml.InkML;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Form
|
||||
{
|
||||
public class ApplicationServiceContext
|
||||
{
|
||||
public static IServiceProvider ServiceProvider = null!;
|
||||
public static IContext Context = null!;
|
||||
public static IConfiguration Configuration;
|
||||
public static ILocaleMessageManager localeMessageManager = null!;
|
||||
public static ISecurityMasterManager securityMasterManager = null!;
|
||||
public ApplicationServiceContext(IServiceProvider serviceProvider, IContext context, IConfiguration configuration)
|
||||
{
|
||||
ServiceProvider = serviceProvider;
|
||||
Context = context;
|
||||
Context.DefaultVersion = configuration.GetValue<string>("CloudBuilder:DefaultVersion")!;
|
||||
Context.DefaultCopyright = configuration.GetValue<string>("CloudBuilder:DefaultCopyright")!;
|
||||
Context.DefaultTitle = configuration.GetValue<string>("CloudBuilder:DefaultTitle")!;
|
||||
|
||||
Configuration = configuration;
|
||||
localeMessageManager = ServiceProvider.GetService<ILocaleMessageManager>()!;
|
||||
securityMasterManager = ServiceProvider.GetService<ISecurityMasterManager>()!;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.DatabaseAccessor;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using CloudBuilder.Core.DependencyInjection.EFCore;
|
||||
using CloudBuilder.Core.DependencyInjection.Request;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using CloudBuilder.Core.Service;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Form
|
||||
{
|
||||
[PackLevel(40)]
|
||||
public class FormPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Business;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将窗体和用户控件添加到依赖注入服务容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
//注入窗体
|
||||
var baseType = typeof(IForm);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.BaseType?.Name != "AbstractPage" && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.TryAddKeyedScoped(implementType, implementType.Name);
|
||||
}
|
||||
|
||||
//注入Page
|
||||
baseType = typeof(IForm);
|
||||
|
||||
allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.BaseType?.Name == "AbstractPage" && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
//主界面窗口及弹出的窗口,要使用Transient注入,每次打开都要创建新的实例
|
||||
//注意要清空之前的自定义属性。BindingContextAttributeProvider
|
||||
services.TryAddKeyedTransient(implementType.BaseType, implementType.Name, implementType);
|
||||
//services.TryAddKeyedScoped(implementType.BaseType, implementType.Name, implementType);
|
||||
}
|
||||
|
||||
//注入用户控件
|
||||
baseType = typeof(IUserControl);
|
||||
types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.TryAddKeyedScoped(implementType, implementType.Name);
|
||||
}
|
||||
|
||||
//services.TryAddSingleton<ApplicationServiceContext>();
|
||||
services.TryAddScoped<ApplicationServiceContext>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Form
|
||||
{
|
||||
public interface IForm
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Form
|
||||
{
|
||||
public interface IUserControl
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Policy
|
||||
{
|
||||
public interface IPolicy
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
||||
using CloudBuilder.Core.DatabaseAccessor;
|
||||
using CloudBuilder.Core.DependencyInjection.EFCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using CloudBuilder.Core.Service;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Policy
|
||||
{
|
||||
[PackLevel(10)]
|
||||
public class PolicyPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Business;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将窗体和用户控件添加到依赖注入服务容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
//注入Policy
|
||||
var baseType = typeof(IPolicy);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && baseType.IsAssignableFrom(type))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.TryAddKeyedScoped(implementType, implementType.Name);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using DocumentFormat.OpenXml.Presentation;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Request
|
||||
{
|
||||
public interface IContext
|
||||
{
|
||||
string DefaultVersion { get; set; }
|
||||
string Requestor { get; set; }
|
||||
string UserType { get; set; }
|
||||
DateTime LoginDate { get; set; }
|
||||
string DefaultCopyright { get; set; }
|
||||
string DefaultTitle { get; set; }
|
||||
void SetPermissionInfo(dynamic permissionInfo);
|
||||
dynamic GetPermissionInfo();
|
||||
void SetToken(string token);
|
||||
string GetToken();
|
||||
string GetCulture();
|
||||
void SetCulture(string culture);
|
||||
bool Assert(Func<string, bool> action, string permissionName);
|
||||
bool Assert(Func<string, string, bool> action, string permissionName, string operate);
|
||||
bool Assert(Func<string, Operate, bool> action, string permissionName, Operate operate);
|
||||
bool AssertDependents(Func<string, string, bool> action, string permissionName, string dependents);
|
||||
bool AssertDependents(Func<string, string, Operate, bool> action, string permissionName, string dependents, Operate operate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Request
|
||||
{
|
||||
public interface IRequest
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
using CloudBuilder.Core.DependencyInjection.Request;
|
||||
using CloudBuilder.Core.Request;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/*
|
||||
{
|
||||
"CloudBuilder": {
|
||||
"Culture": "zh-cn",
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Request
|
||||
{
|
||||
public class LoginContext : IContext
|
||||
{
|
||||
public const string CULTURE_EN = "en";
|
||||
private string culture = null!;
|
||||
private string token = null!;
|
||||
private dynamic permissionInfo = null!;
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public string DefaultVersion { get; set; }
|
||||
public string Requestor { get; set; }
|
||||
public string UserType { get; set; }
|
||||
public DateTime LoginDate { get; set; }
|
||||
public string DefaultCopyright { get; set; }
|
||||
|
||||
public string DefaultTitle { get; set; }
|
||||
|
||||
public LoginContext(IConfiguration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
|
||||
culture = this.configuration["CloudBuilder:Culture"]!;
|
||||
DefaultVersion = this.configuration["CloudBuilder:DefaultVersion"] ?? "Ver.0.0.0.1";
|
||||
Requestor = string.Empty;
|
||||
LoginDate = DateTime.Now;
|
||||
DefaultCopyright = this.configuration["CloudBuilder:DefaultCopyright"] ?? "版权所有 © 2025 有家相册";
|
||||
}
|
||||
|
||||
public bool Assert(Func<string, bool> action, string permissionName)
|
||||
{
|
||||
return action(permissionName);
|
||||
}
|
||||
|
||||
public bool Assert(Func<string, string, bool> action, string permissionName, string operate)
|
||||
{
|
||||
return action(permissionName, operate);
|
||||
}
|
||||
|
||||
public bool Assert(Func<string, Operate, bool> action, string permissionName, Operate operate)
|
||||
{
|
||||
return action(permissionName, operate);
|
||||
}
|
||||
|
||||
public bool AssertDependents(Func<string, string, bool> action, string permissionName, string dependents)
|
||||
{
|
||||
return action(permissionName, dependents);
|
||||
}
|
||||
|
||||
public bool AssertDependents(Func<string, string, Operate, bool> action, string permissionName, string dependents, Operate operate)
|
||||
{
|
||||
return action(permissionName, dependents, operate);
|
||||
}
|
||||
|
||||
public string GetCulture()
|
||||
{
|
||||
return this.culture;
|
||||
}
|
||||
|
||||
public void SetCulture(string culture)
|
||||
{
|
||||
this.culture = culture;
|
||||
}
|
||||
|
||||
public string GetToken()
|
||||
{
|
||||
return this.token;
|
||||
}
|
||||
|
||||
public void SetToken(string token)
|
||||
{
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public dynamic GetPermissionInfo()
|
||||
{
|
||||
return this.permissionInfo;
|
||||
}
|
||||
|
||||
public void SetPermissionInfo(dynamic permissionInfo)
|
||||
{
|
||||
this.permissionInfo = permissionInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Request
|
||||
{
|
||||
[PackLevel(35)]
|
||||
public class RequestPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Business;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将请求添加到依赖注入服务容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
//注入请求
|
||||
services.AddHttpClient();
|
||||
|
||||
var baseType = typeof(IRequest);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
/*TryAddKeyedScoped Scoped(作用域) 每个请求(或作用域)内是同一个实例,不同请求之间不同。
|
||||
* 在 WinForms 应用程序中,依赖注入(DI)的 作用域(Scoped)生命周期 不像 ASP.NET Core 那样自动按 HTTP 请求划分作用域,
|
||||
* 而是需要手动管理作用域。
|
||||
* 在 WinForms 应用程序中,如果没有使用手动管理作用域,那么相关于全局作用域,类似于单例又不同于单例(手动管理作用域)。
|
||||
* 在 WinForms 中使用 Scoped 服务
|
||||
using (var scope1 = ServiceProvider.CreateScope())
|
||||
{
|
||||
AuthRequest authRequest = scope1.ServiceProvider.GetRequiredKeyedService<AuthRequest>(typeof(AuthRequest).Name);
|
||||
string pubKeyc = await authRequest.GetRsaPublicKey();
|
||||
}
|
||||
//下面的对应的是上面的调用方式
|
||||
//services.TryAddKeyedScoped(implementType, implementType.Name);
|
||||
现在修改为下面这样调用方式 IServiceRequest serviceRequest
|
||||
// IAuthRequest authRequest = ServiceProvider.GetService<IAuthRequest>()!;
|
||||
*/
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
//AuthRequest要对应AuthService的方法,所以注入名字
|
||||
services.TryAddKeyedScoped(implementType, implementType.Name);
|
||||
|
||||
//ServiceRequest(注入接口)
|
||||
|
||||
services.TryAddScoped(implementType.GetInterfaces().FirstOrDefault(), implementType);
|
||||
}
|
||||
|
||||
//因为每个用户都是独立的,因此不能注册为单一的
|
||||
//services.TryAddSingleton<IContext, LoginContext>();
|
||||
//客户端的生命周期是静态的,唯一的
|
||||
|
||||
//注册为作用域生命周期
|
||||
services.TryAddScoped<IContext, LoginContext>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using CloudBuilder.Core.Service;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Service
|
||||
{
|
||||
[PackLevel(20)]
|
||||
public class ServicePack : CloudBuilderPack
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Business;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将服务添加到依赖注入服务容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
var baseType = typeof(IService);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
services.TryAddScoped(implementType.GetInterfaces().FirstOrDefault(), implementType);
|
||||
}
|
||||
|
||||
// IAuthService authRequest = ServiceProvider.GetService<IAuthService>()!;
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Task
|
||||
{
|
||||
public interface IScheduleTask
|
||||
{
|
||||
void Run(Dictionary<string, string> bodyDict);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using CloudBuilder.Core.Application.Builders;
|
||||
using CloudBuilder.Core.Application.Packs;
|
||||
using CloudBuilder.Core.Extensions;
|
||||
using CloudBuilder.Core.Service;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace CloudBuilder.Core.DependencyInjection.Task
|
||||
{
|
||||
[PackLevel(20)]
|
||||
public class ScheduleTaskPack : CloudBuilderPack
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 模块级别
|
||||
/// </summary>
|
||||
public override PackLevel Level => PackLevel.Business;
|
||||
|
||||
/// <summary>
|
||||
/// 获取 模块启动顺序,模块启动的顺序先按级别启动,级别内部再按此顺序启动
|
||||
/// </summary>
|
||||
public override int Order => 2;
|
||||
|
||||
/// <summary>
|
||||
/// 将服务添加到依赖注入服务容器中
|
||||
/// </summary>
|
||||
/// <param name="services">依赖注入服务容器</param>
|
||||
/// <returns></returns>
|
||||
public override IServiceCollection AddServices(IServiceCollection services, CloudBuilderBuilder builder)
|
||||
{
|
||||
var baseType = typeof(IScheduleTask);
|
||||
|
||||
var allAssemblies = CloudBuilderBuilder.GetAssemblies();
|
||||
|
||||
var types = allAssemblies
|
||||
.SelectMany(a => a.DefinedTypes)
|
||||
.Select(type => type.AsType())
|
||||
.Where(type => type != baseType && type.HasImplementedRawGeneric(baseType))
|
||||
.Where(x => x.IsClass).ToList();
|
||||
|
||||
|
||||
foreach (var implementType in types)
|
||||
{
|
||||
//使用TryAddScoped可以避免同一个接口注册多个实现
|
||||
|
||||
//使用AddScoped可以同一个接口注册多个实现
|
||||
//即使容器中原本就有IOrder服务,也会再添加一个IOrder服务进去,这样一来容器中就会存在多个IOrder服务
|
||||
//在需要获取服务的地方注入IOrder服务时,默认会得到最后一个注册的IOrder服务实例
|
||||
//IEnumerable<IOrder> orders获取所有的IOrder服务
|
||||
services.AddScoped(baseType, implementType);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user