添加缓存模块
This commit is contained in:
@@ -19,6 +19,21 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描全部
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddAutoIocServer(this IServiceCollection services)
|
||||
{
|
||||
var assemblys= AppDomain.CurrentDomain.GetAssemblies();
|
||||
foreach (var a in assemblys)
|
||||
{
|
||||
RegIoc(services, a);
|
||||
}
|
||||
return services;
|
||||
}
|
||||
private static void RegIoc(IServiceCollection services, Assembly assembly)
|
||||
{
|
||||
foreach (var type in assembly.GetTypes())
|
||||
@@ -27,13 +42,14 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
|
||||
if (serviceAttribute is not null)
|
||||
{
|
||||
//泛型类需要单独进行处理
|
||||
//情况1:使用自定义[AppService(ServiceType = typeof(注册抽象或者接口))],手动去注册,放type即可
|
||||
var serviceType = serviceAttribute.ServiceType;
|
||||
//情况2 自动去找接口,如果存在就是接口,如果不存在就是本身
|
||||
if (serviceType == null)
|
||||
{
|
||||
//获取最靠近的接口
|
||||
var firstInter = type.GetInterfaces().FirstOrDefault();
|
||||
var firstInter = type .GetInterfaces().LastOrDefault();
|
||||
if (firstInter is null)
|
||||
{
|
||||
serviceType = type;
|
||||
|
||||
@@ -16,4 +16,8 @@
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Microsoft\Extensions\Hosting\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.AspNetCore
|
||||
{
|
||||
internal class YiFrameworkAspNetCoreModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
services.AddAutoIocServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Yi.Framework.Caching.MemoryCache
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Caching.MemoryCache
|
||||
{
|
||||
public class MemoryCacheClient : CacheInvoker
|
||||
{
|
||||
private IMemoryCache _client;
|
||||
public MemoryCacheClient()
|
||||
{
|
||||
_client = new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions());
|
||||
}
|
||||
public override bool Exits(string key)
|
||||
{
|
||||
return _client.TryGetValue(key, out var _);
|
||||
}
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
return _client.Get<T>(key);
|
||||
}
|
||||
public override bool Set<T>(string key, T item)
|
||||
{
|
||||
return _client.Set(key, item) is not null;
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T item, TimeSpan time)
|
||||
{
|
||||
return _client.Set(key, item, time) is not null;
|
||||
}
|
||||
|
||||
public override long Del(string key)
|
||||
{
|
||||
_client.Remove(key);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Yi.Framework.Caching.MemoryCache": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "https://localhost:53045;http://localhost:53046"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
@@ -6,4 +6,13 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Caching\Yi.Framework.Caching.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Caching.MemoryCache;
|
||||
using Yi.Framework.Caching;
|
||||
|
||||
namespace Yi.Framework.Ddd
|
||||
{
|
||||
public class YiFrameworkCachingMemoryCacheModule:IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
services.AddSingleton<CacheInvoker, MemoryCacheClient>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Yi.Framework.Caching.Redis
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
165
src/Yi.Framework/Yi.Framework.Caching.Redis/RedisCacheClient.cs
Normal file
165
src/Yi.Framework/Yi.Framework.Caching.Redis/RedisCacheClient.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Caching;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Yi.Framework.Caching.Redis;
|
||||
using static CSRedis.CSRedisClient;
|
||||
using CSRedis;
|
||||
|
||||
namespace Yi.Framework.Caching.Redis
|
||||
{
|
||||
public class RedisCacheClient : CacheInvoker
|
||||
{
|
||||
private readonly CachingConnOptions _RedisOptions;
|
||||
|
||||
private CSRedisClient _client;
|
||||
|
||||
public RedisCacheClient(IOptionsMonitor<CachingConnOptions> redisConnOptions)
|
||||
{
|
||||
this._RedisOptions = redisConnOptions.CurrentValue;
|
||||
_client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={_RedisOptions.DB}");
|
||||
}
|
||||
public override bool Exits(string key)
|
||||
{
|
||||
return _client.Exists(key);
|
||||
}
|
||||
public override T Get<T>(string key)
|
||||
{
|
||||
return _client.Get<T>(key);
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T data, TimeSpan time)
|
||||
{
|
||||
return _client.Set(key, data, time);
|
||||
}
|
||||
|
||||
public override bool Set<T>(string key, T data)
|
||||
{
|
||||
return _client.Set(key, data);
|
||||
}
|
||||
|
||||
public override long Del(string key)
|
||||
{
|
||||
return _client.Del(key);
|
||||
}
|
||||
|
||||
public override bool HSet(string key, string fieId, object data)
|
||||
{
|
||||
return _client.HSet(key, fieId, data);
|
||||
}
|
||||
|
||||
public override bool HSet(string key, string fieId, object data, TimeSpan time)
|
||||
{
|
||||
var res = _client.HSet(key, fieId, data);
|
||||
var res2 = _client.Expire(key, time);
|
||||
return res && res2;
|
||||
}
|
||||
|
||||
public override T HGet<T>(string key, string field)
|
||||
{
|
||||
return _client.HGet<T>(key, field);
|
||||
}
|
||||
|
||||
|
||||
public override long HDel(string key, params string[] par)
|
||||
{
|
||||
return _client.HDel(key, par);
|
||||
}
|
||||
|
||||
public override long HLen(string key)
|
||||
{
|
||||
return _client.HLen(key);
|
||||
}
|
||||
|
||||
public override Dictionary<string, string> HGetAll(string key)
|
||||
{
|
||||
return _client.HGetAll(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 简单发布
|
||||
/// </summary>
|
||||
/// <param name="channel"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public override long Publish(string channel, string message)
|
||||
{
|
||||
return _client.Publish(channel, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 简单订阅:广播,无持久化,需要Publish写入队列
|
||||
/// </summary>
|
||||
/// <param name="channels"></param>
|
||||
/// <returns></returns>
|
||||
public override SubscribeObject Subscribe(params (string, Action<SubscribeMessageEventArgs>)[] channels)
|
||||
{
|
||||
return _client.Subscribe(channels);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多端争抢模式订阅,需要Lpush写入队列
|
||||
/// </summary>
|
||||
/// <param name="listKey"></param>
|
||||
/// <param name="onMessage"></param>
|
||||
/// <returns></returns>
|
||||
public override SubscribeListObject SubscribeList(string listKey, Action<string> onMessage)
|
||||
{
|
||||
return _client.SubscribeList(listKey, onMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多端非争抢模式订阅,需要Lpush写入队列
|
||||
/// </summary>
|
||||
/// <param name="listKey"></param>
|
||||
/// <param name="clientId"></param>
|
||||
/// <param name="onMessage"></param>
|
||||
/// <returns></returns>
|
||||
public override SubscribeListBroadcastObject SubscribeListBroadcast(string listKey, string clientId, Action<string> onMessage)
|
||||
{
|
||||
return _client.SubscribeListBroadcast(listKey, clientId, onMessage);
|
||||
}
|
||||
|
||||
public override bool LSet(string key, long index, object value)
|
||||
{
|
||||
return _client.LSet(key, index, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 列表插入头部
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public override long LPush<T>(string key, params T[] value)
|
||||
{
|
||||
return _client.LPush<T>(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列表弹出头部
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public override T LPop<T>(string key)
|
||||
{
|
||||
return _client.LPop<T>(key);
|
||||
}
|
||||
|
||||
public override string[] Keys(string pattern)
|
||||
{
|
||||
return _client.Keys(pattern);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,13 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSRedisCore" Version="3.8.670" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Caching\Yi.Framework.Caching.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Core.Configuration;
|
||||
|
||||
namespace Yi.Framework.Caching.Redis
|
||||
{
|
||||
|
||||
public class YiFrameworkCachingRedisModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
services.Configure<CachingConnOptions>(Appsettings.appConfiguration("CachingConnOptions"));
|
||||
services.AddSingleton<CacheInvoker, RedisCacheClient>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
116
src/Yi.Framework/Yi.Framework.Caching/CacheInvoker.cs
Normal file
116
src/Yi.Framework/Yi.Framework.Caching/CacheInvoker.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Caching
|
||||
{
|
||||
public abstract class CacheInvoker
|
||||
{
|
||||
|
||||
public virtual bool Exits(string key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public virtual T Get<T>(string key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual bool Set<T>(string key, T data, TimeSpan time)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual bool Set<T>(string key, T data)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual long Del(string key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual bool HSet(string key, string fieId, object data)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual bool HSet(string key, string fieId, object data, TimeSpan time)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual T HGet<T>(string key, string field)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public virtual long HDel(string key, params string[] par)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual long HLen(string key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual Dictionary<string, string> HGetAll(string key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 简单发布
|
||||
/// </summary>
|
||||
/// <param name="channel"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public virtual long Publish(string channel, string message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public virtual bool LSet(string key, long index, object value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 列表插入头部
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public virtual long LPush<T>(string key, params T[] value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列表弹出头部
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public virtual T LPop<T>(string key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual string[] Keys(string pattern)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/Yi.Framework/Yi.Framework.Caching/CachingConnOptions.cs
Normal file
16
src/Yi.Framework/Yi.Framework.Caching/CachingConnOptions.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Caching
|
||||
{
|
||||
public class CachingConnOptions
|
||||
{
|
||||
public string? Host { get; set; }
|
||||
public int DB { get; set; } = 0;
|
||||
public int Prot { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Yi.Framework.Caching
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
using AutoMapper;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Helper;
|
||||
|
||||
namespace Yi.Framework.Autofac.Extensions
|
||||
{
|
||||
public static class AutoMapperExtensions
|
||||
{
|
||||
public static IServiceCollection AddAutoMapperService(this IServiceCollection services)
|
||||
{
|
||||
//这里会通过反射,扫码全部程序集获取继承Profile的类
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
|
||||
|
||||
var profileList = new List<Type>();
|
||||
assemblies.ForEach(a =>
|
||||
{
|
||||
if (a.FullName is not null)
|
||||
{
|
||||
profileList.AddRange(AssemblyHelper.GetClassByBaseClassesAndInterfaces(a.FullName, typeof(Profile)));
|
||||
}
|
||||
|
||||
});
|
||||
services.AddAutoMapper(profileList.ToArray());
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Autofac.Extensions;
|
||||
|
||||
namespace Yi.Framework.Core.AutoMapper
|
||||
{
|
||||
public class YiFrameworkCoreAutoMapperModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
|
||||
//添加全局自动mapper
|
||||
services.AddAutoMapperService();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ using Yi.Framework.Ddd.Repository;
|
||||
|
||||
namespace Yi.Framework.Core.Sqlsugar.Repository
|
||||
{
|
||||
[AppService]
|
||||
[AppService(ServiceType =typeof(IRepository<>))]
|
||||
public class SqlsugarRepository<T> : SimpleClient<T>, IRepository<T> where T : class, new()
|
||||
{
|
||||
public SqlsugarRepository(ISqlSugarClient context) : base(context)
|
||||
|
||||
@@ -15,4 +15,8 @@
|
||||
<ProjectReference Include="..\Yi.Framework.Ddd.Application\Yi.Framework.Ddd.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Options\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using Yi.Framework.Core.Configuration;
|
||||
using Yi.Framework.Core.Options;
|
||||
using Yi.Framework.Core.Sqlsugar.Extensions;
|
||||
using Yi.Framework.Core.Sqlsugar.Repository;
|
||||
using Yi.Framework.Ddd;
|
||||
@@ -17,6 +19,7 @@ namespace Yi.Framework.Core.Sqlsugar
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
services.AddTransient(typeof(IRepository<>), typeof(SqlsugarRepository<>));
|
||||
services.Configure<SqlConnOptions>(Appsettings.appConfiguration("DbConn"));
|
||||
services.AddSqlsugarServer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Core.CurrentUser
|
||||
{
|
||||
public class CurrentUser : ICurrentUser
|
||||
{
|
||||
public bool IsAuthenticated { get; set; }
|
||||
|
||||
public long Id { get; set; }
|
||||
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
public Guid? TenantId { get; set; }
|
||||
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
public bool EmailVerified { get; set; }
|
||||
|
||||
public string PhoneNumber { get; set; } = string.Empty;
|
||||
|
||||
public bool PhoneNumberVerified { get; set; }
|
||||
|
||||
public string[]? Roles { get; set; }
|
||||
|
||||
public string[]? Permission { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Core.CurrentUser
|
||||
{
|
||||
public interface ICurrentUser
|
||||
{
|
||||
public bool IsAuthenticated { get; set; }
|
||||
public long Id { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
|
||||
public Guid? TenantId { get; set; }
|
||||
|
||||
public string Email { get; set; }
|
||||
|
||||
public bool EmailVerified { get; set; }
|
||||
|
||||
public string PhoneNumber { get; set; }
|
||||
|
||||
public bool PhoneNumberVerified { get; set; }
|
||||
|
||||
public string[]? Roles { get; set; }
|
||||
|
||||
public string[]? Permission { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.CurrentUser;
|
||||
|
||||
namespace Yi.Framework.Core.Extensions
|
||||
{
|
||||
public static class CurrentUserExtensions
|
||||
{
|
||||
public static IServiceCollection AddCurrentUserServer(this IServiceCollection services)
|
||||
{
|
||||
return services.AddScoped<ICurrentUser, CurrentUser.CurrentUser>();
|
||||
}
|
||||
|
||||
|
||||
public static IApplicationBuilder UseCurrentUserServer(this IApplicationBuilder app)
|
||||
{
|
||||
return app.UseMiddleware<CurrentUserMiddleware>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class CurrentUserMiddleware
|
||||
{
|
||||
|
||||
private readonly RequestDelegate _next;
|
||||
private ILogger<CurrentUserMiddleware> _logger;
|
||||
public CurrentUserMiddleware(RequestDelegate next, ILogger<CurrentUserMiddleware> logger)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context, ICurrentUser _currentUser)
|
||||
{
|
||||
var authenticateContext = await context.AuthenticateAsync();
|
||||
if (authenticateContext.Principal is null)
|
||||
{
|
||||
_currentUser.IsAuthenticated = false;
|
||||
await _next(context);
|
||||
return;
|
||||
}
|
||||
var claims = authenticateContext.Principal.Claims;
|
||||
//通过鉴权之后,开始赋值
|
||||
_currentUser.IsAuthenticated = true;
|
||||
_currentUser.Id = claims.GetClaim(JwtRegisteredClaimNames.Sid) is null ? 0 : Convert.ToInt64(claims.GetClaim(JwtRegisteredClaimNames.Sid));
|
||||
_currentUser.UserName = claims.GetClaim(SystemConst.UserName) ?? "";
|
||||
_currentUser.Permission = claims.GetClaims(SystemConst.PermissionClaim);
|
||||
_currentUser.TenantId = claims.GetClaim(SystemConst.TenantId) is null ? null : Guid.Parse(claims.GetClaim(SystemConst.TenantId)!);
|
||||
await _next(context);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class ClaimExtension
|
||||
{
|
||||
public static string? GetClaim(this IEnumerable<Claim> claims, string type)
|
||||
{
|
||||
return claims.Where(c => c.Type == type).Select(c => c.Value).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static string[]? GetClaims(this IEnumerable<Claim> claims, string type)
|
||||
{
|
||||
return claims.Where(c => c.Type == type).Select(c => c.Value).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/Yi.Framework/Yi.Framework.Core/Helper/AssemblyHelper.cs
Normal file
65
src/Yi.Framework/Yi.Framework.Core/Helper/AssemblyHelper.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using AutoMapper.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Core.Helper
|
||||
{
|
||||
public static class AssemblyHelper
|
||||
{
|
||||
|
||||
public static List<Assembly> GetReferanceAssemblies(this AppDomain domain)
|
||||
{
|
||||
var list = new List<Assembly>();
|
||||
domain.GetAssemblies().ToList().ForEach(i =>
|
||||
{
|
||||
GetReferanceAssemblies(i, list);
|
||||
});
|
||||
return list;
|
||||
}
|
||||
private static void GetReferanceAssemblies(Assembly assembly, List<Assembly> list)
|
||||
{
|
||||
assembly.GetReferencedAssemblies().ToList().ForEach(i =>
|
||||
{
|
||||
var ass = Assembly.Load(i);
|
||||
if (!list.Contains(ass))
|
||||
{
|
||||
list.Add(ass);
|
||||
GetReferanceAssemblies(ass, list);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static List<Type> GetClass(string assemblyFile, string? className = null, string? spaceName = null)
|
||||
{
|
||||
Assembly assembly = Assembly.Load(assemblyFile);
|
||||
return assembly.GetTypes().Where(m => m.IsClass
|
||||
&& className == null ? true : m.Name == className
|
||||
&& spaceName == null ? true : m.Namespace == spaceName
|
||||
).ToList();
|
||||
}
|
||||
|
||||
public static List<Type> GetClassByBaseClassesAndInterfaces(string assemblyFile, Type type)
|
||||
{
|
||||
Assembly assembly = Assembly.Load(assemblyFile);
|
||||
|
||||
List<Type> resList = new List<Type>();
|
||||
|
||||
List<Type> typeList = assembly.GetTypes().Where(m => m.IsClass).ToList();
|
||||
foreach (var t in typeList)
|
||||
{
|
||||
var data = t.BaseClassesAndInterfaces();
|
||||
if (data.Contains(type))
|
||||
{
|
||||
resList.Add(t);
|
||||
}
|
||||
|
||||
}
|
||||
return resList;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
14
src/Yi.Framework/Yi.Framework.Core/Options/SqlConnOptions.cs
Normal file
14
src/Yi.Framework/Yi.Framework.Core/Options/SqlConnOptions.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Core.Options
|
||||
{
|
||||
public class SqlConnOptions
|
||||
{
|
||||
public string WriteUrl { get; set; } = string.Empty;
|
||||
public List<string>? ReadUrl { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Extensions\" />
|
||||
<Folder Include="Module\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
|
||||
<PackageReference Include="Panda.DynamicWebApi" Version="1.2.1" />
|
||||
<PackageReference Include="StartupModules" Version="4.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -8,6 +8,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Core.Configuration;
|
||||
using Yi.Framework.Core.Extensions;
|
||||
using Yi.Framework.Core.Model;
|
||||
|
||||
namespace Yi.Framework.Core
|
||||
@@ -25,6 +26,7 @@ namespace Yi.Framework.Core
|
||||
//添加全局配置类
|
||||
services.AddSingleton(new Appsettings(context.Configuration));
|
||||
|
||||
|
||||
//全局日志
|
||||
GobalLogModel.SqlLogEnable = Appsettings.appBool("SqlLog_Enable");
|
||||
GobalLogModel.LoginCodeEnable = Appsettings.appBool("LoginCode_Enable");
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace Yi.Framework.Ddd
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
|
||||
using AspNetCore.Microsoft.AspNetCore.Builder;
|
||||
using Panda.DynamicWebApi;
|
||||
using System.Reflection;
|
||||
using Yi.Framework.Application;
|
||||
using Yi.Framework.Autofac.Extensions;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.Core.AutoMapper;
|
||||
using Yi.Framework.Core.Extensions;
|
||||
using Yi.Framework.Core.Sqlsugar;
|
||||
using Yi.Framework.Core.Sqlsugar.Repository;
|
||||
@@ -12,19 +15,25 @@ using Yi.Framework.Web;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.WebHost.UseUrls(builder.Configuration.GetValue<string>("StartUrl"));
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
|
||||
builder.UseYiModules(
|
||||
typeof(YiFrameworkCoreModule).Assembly,
|
||||
typeof(YiFrameworkCoreAutoMapperModule).Assembly,
|
||||
typeof(YiFrameworkDddModule).Assembly,
|
||||
typeof(YiFrameworkCoreSqlsugarModule).Assembly
|
||||
);
|
||||
|
||||
//ʹ<><CAB9>autofac
|
||||
builder.Host.UseAutoFacServerProviderFactory();
|
||||
|
||||
//<2F><><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>붯̬api
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddDynamicWebApi();
|
||||
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>swagger
|
||||
builder.Services.AddSwaggerServer<YiFrameworkApplicationModule>();
|
||||
//builder.Services.AddAutoIocServer("Yi.Framework.Core.Sqlsugar");
|
||||
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yi.Framework.Application\Yi.Framework.Application.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core.Autofac\Yi.Framework.Core.Autofac.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core.AutoMapper\Yi.Framework.Core.AutoMapper.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core.Sqlsugar\Yi.Framework.Core.Sqlsugar.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
|
||||
//程序启动地址,*代表全部网口
|
||||
"StartUrl": "http://*:19002",
|
||||
|
||||
//数据库列表
|
||||
"DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ],
|
||||
|
||||
@@ -27,7 +27,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.BackgroundJobs
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Ddd", "Yi.Framework.Ddd.Application\Yi.Framework.Ddd.csproj", "{949F35A7-36E4-4080-9940-24BE52532078}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Autofac", "Yi.Framework.Autofac\Yi.Framework.Autofac.csproj", "{63BA134E-9D23-4EB8-87E4-B45B33D954F5}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Core.Autofac", "Yi.Framework.Core.Autofac\Yi.Framework.Core.Autofac.csproj", "{63BA134E-9D23-4EB8-87E4-B45B33D954F5}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.AspNetCore", "Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj", "{2F5E5843-14FB-48F1-AEB0-B9FFE103B972}"
|
||||
EndProject
|
||||
@@ -41,7 +41,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Caching.Memory
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Uow", "Yi.Framework.Uow\Yi.Framework.Uow.csproj", "{3D83BE69-71BB-43BE-B3F1-A532215561CD}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Core.Sqlsugar", "Yi.Framework.Core.Sqlsugar\Yi.Framework.Core.Sqlsugar.csproj", "{58F4071D-66B7-4839-A247-79AF0E4E1C8E}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Yi.Framework.Core.Sqlsugar", "Yi.Framework.Core.Sqlsugar\Yi.Framework.Core.Sqlsugar.csproj", "{58F4071D-66B7-4839-A247-79AF0E4E1C8E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Yi.Framework.Core.AutoMapper", "Yi.Framework.Core.AutoMapper\Yi.Framework.Core.AutoMapper.csproj", "{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -117,6 +119,10 @@ Global
|
||||
{58F4071D-66B7-4839-A247-79AF0E4E1C8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{58F4071D-66B7-4839-A247-79AF0E4E1C8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{58F4071D-66B7-4839-A247-79AF0E4E1C8E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -139,6 +145,7 @@ Global
|
||||
{67CF07AB-2A72-4B36-A3A5-4CEB82B7C43C} = {EEF5F221-0E32-4A3D-B647-B4B5E7305806}
|
||||
{3D83BE69-71BB-43BE-B3F1-A532215561CD} = {5F2B846D-96CE-400A-878E-220498F4EE31}
|
||||
{58F4071D-66B7-4839-A247-79AF0E4E1C8E} = {5F2B846D-96CE-400A-878E-220498F4EE31}
|
||||
{DFD34702-2EF6-4ECC-AE6E-9A1A3885BD26} = {5F2B846D-96CE-400A-878E-220498F4EE31}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {6C1A3808-0F4F-43FB-A9FE-5F27A3BB2ECF}
|
||||
|
||||
Reference in New Issue
Block a user