Regions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Serilog;
  5. using SMP.Common.Auth;
  6. using System.Reflection;
  7. using XYY.Core.Standard.Data.Infrastructure;
  8. using Microsoft.AspNetCore.Cors;
  9. using Microsoft.Extensions.Logging;
  10. using SMP.Common.exception;
  11. using XYY.Core.Standard.AliYun;
  12. using XYY.Core.Standard.Data.Redis;
  13. using SMP.Common.Mq;
  14. public static class Regions
  15. {
  16. /// <summary>
  17. /// 日志注册
  18. /// </summary>
  19. /// <param name="services"></param>
  20. /// <returns></returns>
  21. public static IServiceCollection AddSeqLogging(this IServiceCollection services)
  22. {
  23. //日志使用Seq
  24. var log = new LoggerConfiguration()
  25. .MinimumLevel.Warning()
  26. .MinimumLevel.Override("LogginService", Serilog.Events.LogEventLevel.Warning)
  27. .Enrich.FromLogContext()
  28. .WriteTo.Seq("http://47.244.232.78:5341")
  29. .CreateLogger();
  30. services.AddLogging(logging =>
  31. {
  32. logging.AddSerilog(log);
  33. });
  34. return services;
  35. }
  36. public static IServiceCollection AddLog4(this IServiceCollection services)
  37. {
  38. services.AddLogging(logging =>
  39. {
  40. logging.AddLog4Net();
  41. });
  42. return services;
  43. }
  44. public static class DefaultConfig
  45. {
  46. /// <summary>
  47. /// DBConnectionStrings:SqlService
  48. /// </summary>
  49. public static string SqlServiceConnectionKey => "DBConnectionStrings:SMPSqlService";
  50. public static string RedisConnectionKey => "DBConnectionStrings:RedisCacheUrl";
  51. public static string RedisPwd => "DBConnectionStrings:RedisPwd";
  52. public static string AliYunBucketName => "aliyun:AliYunBucketName";
  53. public static string AliYunEndPoint => "aliyun:aliYunEndPoint";
  54. public static string AliYunKeyId => "aliyun:aliYunKeyId";
  55. public static string AliYunKeySecret => "aliyun:aliYunKeySecret";
  56. public static string AliYunBasePath => "aliyun:BasePath";
  57. public static string AliYunPubEndPoint => "aliyun:aliYunPubEndPoint";
  58. public static string RabbitMqConnectionKey = "DBConnectionStrings:MqConnection";
  59. }
  60. public enum IDType
  61. {
  62. Scoped = 0,
  63. Singleton = 1,
  64. Transient = 2
  65. }
  66. public enum DistributedCacheType
  67. {
  68. Memory = 0,
  69. Redis = 1
  70. }
  71. public static IServiceCollection AddRabbitMqService(this IServiceCollection services, IConfiguration Configuration, IDType idType)
  72. {
  73. if (string.IsNullOrEmpty(Configuration[DefaultConfig.RabbitMqConnectionKey]))
  74. throw new Exception($"未找到配置项{DefaultConfig.RabbitMqConnectionKey}");
  75. string mqConnection = Configuration[DefaultConfig.RabbitMqConnectionKey];
  76. services.AddSingleton(x => new MQManager(mqConnection));
  77. //AddRepositorySerice(services, idType);
  78. return services;
  79. }
  80. public static IServiceCollection AddCache(this IServiceCollection services, IConfiguration Configuration, DistributedCacheType DistributedCacheType)
  81. {
  82. string redisConnection = Configuration[DefaultConfig.RedisConnectionKey];
  83. string redisPwd = Configuration[DefaultConfig.RedisPwd];
  84. if (DistributedCacheType == DistributedCacheType.Redis
  85. )
  86. {
  87. if (redisConnection == null)
  88. throw new Exception($"Redis 连接未配置{redisConnection}");
  89. if (redisPwd == null)
  90. throw new Exception($"Redis 密码未配置{redisPwd}");
  91. }
  92. //系统缓存接入
  93. switch (DistributedCacheType)
  94. {
  95. case DistributedCacheType.Memory:
  96. services.AddDistributedMemoryCache();
  97. break;
  98. case DistributedCacheType.Redis:
  99. services.AddDistributedRedisCache(x =>
  100. {
  101. x.Configuration = redisConnection;
  102. x.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions
  103. {
  104. Password = redisPwd,
  105. AbortOnConnectFail = false
  106. };
  107. x.ConfigurationOptions.EndPoints.Add(redisConnection);
  108. });
  109. break;
  110. }
  111. //原生Redis客户端
  112. services.AddSingleton(x => new SMP.Common.WebRegion.RedisHelper(redisConnection, "Redis", redisPwd, 0));
  113. return services;
  114. }
  115. /// <summary>
  116. /// 授权
  117. /// </summary>
  118. /// <param name="services"></param>
  119. /// <returns></returns>
  120. public static IServiceCollection AddAuthentication(this IServiceCollection services)
  121. {
  122. return services;
  123. }
  124. public static void UseBasicAuthentication(this IApplicationBuilder app)
  125. {
  126. app.UseMiddleware<BasicAuthenticationMiddleware>();
  127. }
  128. public static void UseCore(this IServiceCollection services, string so)
  129. {
  130. services.AddCors(option => option.AddPolicy(so, policy =>
  131. policy.AllowAnyHeader().AllowAnyOrigin().WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")));
  132. }
  133. /// <summary>
  134. /// 数据服务注册
  135. /// </summary>
  136. /// <param name="services"></param>
  137. /// <param name="idType"></param>
  138. /// <param name="Configuration"></param>
  139. /// <returns></returns>
  140. public static IServiceCollection AddDataService(this IServiceCollection services, IConfiguration Configuration, IDType idType)
  141. {
  142. if (string.IsNullOrEmpty(Configuration[DefaultConfig.SqlServiceConnectionKey]))
  143. throw new Exception($"未找到配置项{DefaultConfig.SqlServiceConnectionKey}");
  144. services.AddDB(Configuration[DefaultConfig.SqlServiceConnectionKey]);
  145. AddRepositorySerice(services, idType);
  146. return services;
  147. }
  148. public static IServiceCollection AddAppService(this IServiceCollection services, IDType idType)
  149. {
  150. var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
  151. var referencedAssemblies = System.IO.Directory.GetFiles(path, "SMP.Service.dll")
  152. .Select(Assembly.LoadFrom).ToArray();
  153. var types = referencedAssemblies
  154. .SelectMany(a => a.DefinedTypes)
  155. .Select(type => type.AsType())
  156. .Where(x =>
  157. x.Name.EndsWith("Service")
  158. || x.Name.EndsWith("Api")
  159. || x.Name.EndsWith("ServiceBillResolver")
  160. ).ToArray();
  161. var implementTypes = types.Where(x => x.IsClass).ToArray();
  162. var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
  163. foreach (var implementType in implementTypes)
  164. {
  165. var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
  166. if (interfaceType != null)
  167. {
  168. switch (idType)
  169. {
  170. case IDType.Scoped:
  171. services.AddScoped(interfaceType, implementType);
  172. break;
  173. case IDType.Singleton:
  174. services.AddSingleton(interfaceType, implementType);
  175. break;
  176. case IDType.Transient:
  177. services.AddTransient(interfaceType, implementType);
  178. break;
  179. }
  180. }
  181. }
  182. //services.AddTransient<IServiceBillResolve, >
  183. return services;
  184. }
  185. public static void AddAliyunService(this IServiceCollection services, IConfiguration Configuration)
  186. {
  187. if (!string.IsNullOrEmpty(Configuration[DefaultConfig.AliYunPubEndPoint]))
  188. {
  189. var aliYunConfig = new AliYunConfig
  190. {
  191. AliYunBucketName = Configuration[DefaultConfig.AliYunBucketName],
  192. AliYunEndPoint = Configuration[DefaultConfig.AliYunEndPoint],
  193. AliYunKeyId = Configuration[DefaultConfig.AliYunKeyId],
  194. AliYunKeySecret = Configuration[DefaultConfig.AliYunKeySecret],
  195. BasePath = Configuration[DefaultConfig.AliYunBasePath],
  196. CDNEndPoint = Configuration[DefaultConfig.AliYunPubEndPoint],
  197. };
  198. services.AddSingleton<IAliYunPostFileSerivce>(x => new AliYunPostFileSerivce(aliYunConfig));
  199. }
  200. }
  201. /// <summary>
  202. /// 事务注册
  203. /// </summary>
  204. /// <param name="services"></param>
  205. /// <returns></returns>
  206. public static IServiceCollection AddMVCTransfer(this IServiceCollection services)
  207. {
  208. services.ConfigAspectCore("Repository");
  209. services.AddMvcCore(x =>
  210. {
  211. x.Filters.Add<ApiExceptionAcitonFilterLogAttribute>();
  212. x.Filters.Add<SMP.Common.exception.ApiActionFilterAttribute>();
  213. });
  214. return services;
  215. }
  216. /// <summary>
  217. /// 数据服务注册
  218. /// </summary>
  219. /// <param name="services"></param>
  220. /// <param name="idType"></param>
  221. /// <returns></returns>
  222. private static IServiceCollection AddRepositorySerice(this IServiceCollection services, IDType idType)
  223. {
  224. var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
  225. var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll").Where(x =>
  226. (System.IO.Path.GetFileName(x).StartsWith("SMP")
  227. && System.IO.Path.GetFileName(x).Contains("Data")
  228. //&& !System.IO.Path.GetFileName(x).Contains("DS")
  229. ))
  230. .Select(Assembly.LoadFrom).ToArray();
  231. var types = referencedAssemblies
  232. .SelectMany(a => a.DefinedTypes)
  233. .Select(type => type.AsType())
  234. .Where(x =>
  235. x.Name.EndsWith("Repository")
  236. ).ToArray();
  237. var implementTypes = types.Where(x => x.IsClass).ToArray();
  238. var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
  239. foreach (var implementType in implementTypes)
  240. {
  241. var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
  242. if (interfaceType != null)
  243. {
  244. switch (idType)
  245. {
  246. case IDType.Scoped:
  247. services.AddScoped(interfaceType, implementType);
  248. break;
  249. case IDType.Singleton:
  250. services.AddSingleton(interfaceType, implementType);
  251. break;
  252. case IDType.Transient:
  253. services.AddTransient(interfaceType, implementType);
  254. break;
  255. }
  256. }
  257. }
  258. return services;
  259. }
  260. }