123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- using Microsoft.AspNetCore.Builder;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Serilog;
- using SMP.Common.Auth;
- using System.Reflection;
- using XYY.Core.Standard.Data.Infrastructure;
- using Microsoft.AspNetCore.Cors;
- using Microsoft.Extensions.Logging;
- using SMP.Common.exception;
- using XYY.Core.Standard.AliYun;
- using XYY.Core.Standard.Data.Redis;
- public static class Regions
- {
- /// <summary>
- /// 日志注册
- /// </summary>
- /// <param name="services"></param>
- /// <returns></returns>
- public static IServiceCollection AddSeqLogging(this IServiceCollection services)
- {
- //日志使用Seq
- var log = new LoggerConfiguration()
- .MinimumLevel.Warning()
- .MinimumLevel.Override("LogginService", Serilog.Events.LogEventLevel.Warning)
- .Enrich.FromLogContext()
- .WriteTo.Seq("http://47.244.232.78:5341")
- .CreateLogger();
- services.AddLogging(logging =>
- {
- logging.AddSerilog(log);
- });
- return services;
- }
- public static IServiceCollection AddLog4(this IServiceCollection services)
- {
- services.AddLogging(logging =>
- {
- logging.AddLog4Net();
- });
- return services;
- }
- public static class DefaultConfig
- {
- /// <summary>
- /// DBConnectionStrings:SqlService
- /// </summary>
- public static string SqlServiceConnectionKey => "DBConnectionStrings:SMPSqlService";
- public static string RedisConnectionKey => "DBConnectionStrings:RedisCacheUrl";
- public static string RedisPwd => "DBConnectionStrings:RedisPwd";
- public static string AliYunBucketName => "aliyun:AliYunBucketName";
- public static string AliYunEndPoint => "aliyun:aliYunEndPoint";
- public static string AliYunKeyId => "aliyun:aliYunKeyId";
- public static string AliYunKeySecret => "aliyun:aliYunKeySecret";
- public static string AliYunBasePath => "aliyun:BasePath";
- public static string AliYunPubEndPoint => "aliyun:aliYunPubEndPoint";
- }
- public enum IDType
- {
- Scoped = 0,
- Singleton = 1,
- Transient = 2
- }
- public enum DistributedCacheType
- {
- Memory = 0,
- Redis = 1
- }
- public static IServiceCollection AddCache(this IServiceCollection services, IConfiguration Configuration, DistributedCacheType DistributedCacheType)
- {
- string redisConnection = Configuration[DefaultConfig.RedisConnectionKey];
- string redisPwd = Configuration[DefaultConfig.RedisPwd];
- if (DistributedCacheType == DistributedCacheType.Redis
- )
- {
- if (redisConnection == null)
- throw new Exception($"Redis 连接未配置{redisConnection}");
- if (redisPwd == null)
- throw new Exception($"Redis 密码未配置{redisPwd}");
- }
- //系统缓存接入
- switch (DistributedCacheType)
- {
- case DistributedCacheType.Memory:
- services.AddDistributedMemoryCache();
- break;
- case DistributedCacheType.Redis:
- services.AddDistributedRedisCache(x =>
- {
- x.Configuration = redisConnection;
- x.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions
- {
- Password = redisPwd,
- AbortOnConnectFail = false
- };
- x.ConfigurationOptions.EndPoints.Add(redisConnection);
- });
- break;
- }
- //原生Redis客户端
- services.AddSingleton(x => new SMP.Common.WebRegion.RedisHelper(redisConnection, "Redis", redisPwd, 0));
- return services;
- }
- /// <summary>
- /// 授权
- /// </summary>
- /// <param name="services"></param>
- /// <returns></returns>
- public static IServiceCollection AddAuthentication(this IServiceCollection services)
- {
- return services;
- }
- public static void UseBasicAuthentication(this IApplicationBuilder app)
- {
- app.UseMiddleware<BasicAuthenticationMiddleware>();
- }
- public static void UseCore(this IServiceCollection services, string so)
- {
- services.AddCors(option => option.AddPolicy(so, policy =>
- policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")));
- }
- /// <summary>
- /// 数据服务注册
- /// </summary>
- /// <param name="services"></param>
- /// <param name="idType"></param>
- /// <param name="Configuration"></param>
- /// <returns></returns>
- public static IServiceCollection AddDataService(this IServiceCollection services, IConfiguration Configuration, IDType idType)
- {
- if (string.IsNullOrEmpty(Configuration[DefaultConfig.SqlServiceConnectionKey]))
- throw new Exception($"未找到配置项{DefaultConfig.SqlServiceConnectionKey}");
- services.AddDB(Configuration[DefaultConfig.SqlServiceConnectionKey]);
- AddRepositorySerice(services, idType);
- return services;
- }
- public static IServiceCollection AddAppService(this IServiceCollection services, IDType idType)
- {
- var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
- var referencedAssemblies = System.IO.Directory.GetFiles(path, "SMP.Service.dll")
- .Select(Assembly.LoadFrom).ToArray();
- var types = referencedAssemblies
- .SelectMany(a => a.DefinedTypes)
- .Select(type => type.AsType())
- .Where(x =>
- x.Name.EndsWith("Service")
- || x.Name.EndsWith("Api")
- || x.Name.EndsWith("ServiceBillResolver")
- ).ToArray();
- var implementTypes = types.Where(x => x.IsClass).ToArray();
- var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
- foreach (var implementType in implementTypes)
- {
- var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
- if (interfaceType != null)
- {
- switch (idType)
- {
- case IDType.Scoped:
- services.AddScoped(interfaceType, implementType);
- break;
- case IDType.Singleton:
- services.AddSingleton(interfaceType, implementType);
- break;
- case IDType.Transient:
- services.AddTransient(interfaceType, implementType);
- break;
- }
- }
- }
- //services.AddTransient<IServiceBillResolve, >
- return services;
- }
- public static void AddAliyunService(this IServiceCollection services, IConfiguration Configuration)
- {
- if (!string.IsNullOrEmpty(Configuration[DefaultConfig.AliYunPubEndPoint]))
- {
- var aliYunConfig = new AliYunConfig
- {
- AliYunBucketName = Configuration[DefaultConfig.AliYunBucketName],
- AliYunEndPoint = Configuration[DefaultConfig.AliYunEndPoint],
- AliYunKeyId = Configuration[DefaultConfig.AliYunKeyId],
- AliYunKeySecret = Configuration[DefaultConfig.AliYunKeySecret],
- BasePath = Configuration[DefaultConfig.AliYunBasePath],
- CDNEndPoint = Configuration[DefaultConfig.AliYunPubEndPoint],
- };
- services.AddSingleton<IAliYunPostFileSerivce>(x => new AliYunPostFileSerivce(aliYunConfig));
- }
- }
- /// <summary>
- /// 事务注册
- /// </summary>
- /// <param name="services"></param>
- /// <returns></returns>
- public static IServiceCollection AddMVCTransfer(this IServiceCollection services)
- {
- services.ConfigAspectCore("Repository");
- services.AddMvcCore(x =>
- {
- x.Filters.Add<ApiExceptionAcitonFilterLogAttribute>();
- x.Filters.Add<SMP.Common.exception.ApiActionFilterAttribute>();
- });
- return services;
- }
- /// <summary>
- /// 数据服务注册
- /// </summary>
- /// <param name="services"></param>
- /// <param name="idType"></param>
- /// <returns></returns>
- private static IServiceCollection AddRepositorySerice(this IServiceCollection services, IDType idType)
- {
- var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
- var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll").Where(x =>
- (System.IO.Path.GetFileName(x).StartsWith("SMP")
- && System.IO.Path.GetFileName(x).Contains("Data")
- //&& !System.IO.Path.GetFileName(x).Contains("DS")
- ))
- .Select(Assembly.LoadFrom).ToArray();
- var types = referencedAssemblies
- .SelectMany(a => a.DefinedTypes)
- .Select(type => type.AsType())
- .Where(x =>
- x.Name.EndsWith("Repository")
- ).ToArray();
- var implementTypes = types.Where(x => x.IsClass).ToArray();
- var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
- foreach (var implementType in implementTypes)
- {
- var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
- if (interfaceType != null)
- {
- switch (idType)
- {
- case IDType.Scoped:
- services.AddScoped(interfaceType, implementType);
- break;
- case IDType.Singleton:
- services.AddSingleton(interfaceType, implementType);
- break;
- case IDType.Transient:
- services.AddTransient(interfaceType, implementType);
- break;
- }
- }
- }
- return services;
- }
- }
|