ow
This commit is contained in:
1 parent
31fbe199c1
commit
1c0c83af5f
469 files changed
+62544
-5
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user