Regions.cs 9.9 KB

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