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; using SMP.Common.Mq; public static class Regions { /// /// 日志注册 /// /// /// 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 { /// /// DBConnectionStrings:SqlService /// 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 static string RabbitMqConnectionKey = "DBConnectionStrings:MqConnection"; } public enum IDType { Scoped = 0, Singleton = 1, Transient = 2 } public enum DistributedCacheType { Memory = 0, Redis = 1 } public static IServiceCollection AddRabbitMqService(this IServiceCollection services, IConfiguration Configuration, IDType idType) { if (string.IsNullOrEmpty(Configuration[DefaultConfig.RabbitMqConnectionKey])) throw new Exception($"未找到配置项{DefaultConfig.RabbitMqConnectionKey}"); string mqConnection = Configuration[DefaultConfig.RabbitMqConnectionKey]; services.AddSingleton(x => new MQManager(mqConnection)); //AddRepositorySerice(services, idType); return services; } 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; } /// /// 授权 /// /// /// public static IServiceCollection AddAuthentication(this IServiceCollection services) { return services; } public static void UseBasicAuthentication(this IApplicationBuilder app) { app.UseMiddleware(); } 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"))); } /// /// 数据服务注册 /// /// /// /// /// 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 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(x => new AliYunPostFileSerivce(aliYunConfig)); } } /// /// 事务注册 /// /// /// public static IServiceCollection AddMVCTransfer(this IServiceCollection services) { services.ConfigAspectCore("Repository"); services.AddMvcCore(x => { x.Filters.Add(); x.Filters.Add(); }); return services; } /// /// 数据服务注册 /// /// /// /// 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; } }