ServiceRegion.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using System;
  4. using System.Linq;
  5. using System.Reflection;
  6. using XYY.Core.Standard.ExcelHelper.MSExcelHelper;
  7. using XYY.Core.Standard.AliYun;
  8. using XYY.Core.Standard.Data.Infrastructure;
  9. using EasyNetQ;
  10. using XYY.TaskTrack.Standard;
  11. using Serilog;
  12. using StackExchange.Redis;
  13. using XYY.Core.Standard.Data.Redis;
  14. using XYY.Core.Standard.FiddlerClient;
  15. namespace XYY.Service.Standard.RegionService
  16. {
  17. public enum DistributedCacheType
  18. {
  19. Memory = 0,
  20. Redis = 1
  21. }
  22. public class ServiceOption
  23. {
  24. public bool NoService
  25. {
  26. get; set;
  27. }
  28. public string PBESConnection
  29. {
  30. get; set;
  31. }
  32. public string MsgESConnection
  33. {
  34. get; set;
  35. }
  36. /// <summary>
  37. /// 是否开启缓存
  38. /// </summary>
  39. public DistributedCacheType CacheType
  40. {
  41. get; set;
  42. }
  43. /// <summary>
  44. /// 是否使用事务支持
  45. /// </summary>
  46. public bool USEDBTransferAsMVC
  47. {
  48. get; set;
  49. }
  50. /// <summary>
  51. /// 传入连接并启用头程信息
  52. /// </summary>
  53. public string FirstESConnection
  54. {
  55. get; set;
  56. }
  57. /// <summary>
  58. /// 手动补充头程数据
  59. /// </summary>
  60. public string FirstESConnection2
  61. {
  62. get; set;
  63. }
  64. /// <summary>
  65. /// 是否启用Rebbit
  66. /// </summary>
  67. public bool UseRabbit
  68. {
  69. get; set;
  70. }
  71. public string FinanceChargesRedisConnection
  72. {
  73. get; set;
  74. }
  75. public string RedisConnection
  76. {
  77. get; set;
  78. }
  79. public bool UseTrackingPUSHRedis
  80. {
  81. get; set;
  82. }
  83. public bool UseFinanceChargesRedis
  84. {
  85. get; set;
  86. }
  87. public string SqlServiceConnection
  88. {
  89. get; set;
  90. }
  91. public AliYunConfig AliYunConfig
  92. {
  93. get; set;
  94. }
  95. public bool OpenChannelApi
  96. {
  97. get; set;
  98. }
  99. /// <summary>
  100. /// 开启捕获
  101. /// </summary>
  102. public bool UseFiddler
  103. {
  104. get; set;
  105. }
  106. }
  107. public static class DefaultConfig
  108. {
  109. /// <summary>
  110. /// DBConnectionStrings:SqlService
  111. /// </summary>
  112. public static string SqlServiceConnectionKey => "DBConnectionStrings:SqlService";
  113. public static string ESUrlKey => "DBConnectionStrings:ESUrl";
  114. public static string AliYunBucketName => "aliyun:AliYunBucketName";
  115. public static string AliYunEndPoint => "aliyun:aliYunEndPoint";
  116. public static string AliYunKeyId => "aliyun:aliYunKeyId";
  117. public static string AliYunKeySecret => "aliyun:aliYunKeySecret";
  118. public static string AliYunBasePath => "aliyun:BasePath";
  119. public static string AliYunPubEndPoint => "aliyun:aliYunPubEndPoint";
  120. public static string RabbitConnectionKey => "DBConnectionStrings:RabbitConnection";
  121. public static string RedisConnectionKey => "DBConnectionStrings:RedisCacheUrl";
  122. }
  123. public static class ServiceRegion
  124. {
  125. public static IServiceCollection AddDataService(this IServiceCollection services)
  126. {
  127. #region 依赖注入
  128. //services.AddScoped<IUserService, UserService>();
  129. string[] regeionTypes = new string[]
  130. {
  131. "XYY.Data.Standard.dll",
  132. "XYY.Data.Standard.User.dll",
  133. "XYY.Data.Standard.Customer.dll",
  134. "XYY.Data.Standard.Charging.dll"
  135. };
  136. var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
  137. var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll").Where(x =>
  138. regeionTypes.Contains(System.IO.Path.GetFileName(x)))
  139. .Select(Assembly.LoadFrom).ToArray();
  140. var types = referencedAssemblies
  141. .SelectMany(a => a.DefinedTypes)
  142. .Select(type => type.AsType())
  143. .Where(x =>
  144. x.Name.EndsWith("Repository")
  145. ).ToArray();
  146. var implementTypes = types.Where(x => x.IsClass).ToArray();
  147. var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
  148. foreach (var implementType in implementTypes)
  149. {
  150. var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
  151. if (interfaceType != null)
  152. services.AddScoped(interfaceType, implementType);
  153. }
  154. #endregion
  155. return services;
  156. }
  157. public static IServiceCollection AddAliyunService(this IServiceCollection services, IConfiguration Configuration)
  158. {
  159. if (!string.IsNullOrEmpty(Configuration[DefaultConfig.AliYunPubEndPoint]))
  160. {
  161. var AliYunConfig = new Core.Standard.AliYun.AliYunConfig
  162. {
  163. AliYunBucketName = Configuration[DefaultConfig.AliYunBucketName],
  164. AliYunEndPoint = Configuration[DefaultConfig.AliYunEndPoint],
  165. AliYunKeyId = Configuration[DefaultConfig.AliYunKeyId],
  166. AliYunKeySecret = Configuration[DefaultConfig.AliYunKeySecret],
  167. BasePath = Configuration[DefaultConfig.AliYunBasePath],
  168. CDNEndPoint = Configuration[DefaultConfig.AliYunPubEndPoint],
  169. };
  170. services.AddSingleton<IAliYunPostFileSerivce>(x => new AliYunPostFileSerivce(AliYunConfig));
  171. }
  172. return services;
  173. }
  174. /// <summary>
  175. /// 添加Service层注入
  176. /// </summary>
  177. /// <param name="services"></param>
  178. /// <param name="serviceName">XYY.Service.Standard.{serviceName}</param>
  179. /// <returns></returns>
  180. public static IServiceCollection AddStandardService(this IServiceCollection services, string serviceName)
  181. {
  182. var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
  183. var referencedAssemblies = System.IO.Directory.GetFiles(path, $"XYY.Service.Standard.{serviceName}.dll")
  184. .Where(x => !System.IO.Path.GetFileName(x).Equals("XYY.Service.Standard.RegionService.dll"))
  185. .Select(Assembly.LoadFrom).ToArray();
  186. var types = referencedAssemblies
  187. .SelectMany(a => a.DefinedTypes)
  188. .Select(type => type.AsType())
  189. .Where(x =>
  190. x.Name.EndsWith("Service")
  191. || x.Name.EndsWith("Api")
  192. ).ToArray();
  193. var implementTypes = types.Where(x => x.IsClass).ToArray();
  194. var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
  195. foreach (var implementType in implementTypes)
  196. {
  197. var allInterfaceTypes = interfaceTypes.Where(x => x.IsAssignableFrom(implementType));
  198. if (allInterfaceTypes?.Any() == true)
  199. {
  200. foreach (var interfaceType in allInterfaceTypes)
  201. {
  202. services.AddScoped(interfaceType, implementType);
  203. }
  204. }
  205. }
  206. return services;
  207. }
  208. /// 若需要支持事务服务、方法计时服务请配置 spectCore.Extensions.DependencyInjection 框架
  209. /// 启动项添加配置: UseServiceProviderFactory(x => new DynamicProxyServiceProviderFactory())
  210. public static IServiceCollection AddXYYService(this IServiceCollection services, ServiceOption serviceOption, IConfiguration Configuration)
  211. {
  212. if (!string.IsNullOrEmpty(Configuration[DefaultConfig.AliYunPubEndPoint]))
  213. {
  214. serviceOption.AliYunConfig = new Core.Standard.AliYun.AliYunConfig
  215. {
  216. AliYunBucketName = Configuration[DefaultConfig.AliYunBucketName],
  217. AliYunEndPoint = Configuration[DefaultConfig.AliYunEndPoint],
  218. AliYunKeyId = Configuration[DefaultConfig.AliYunKeyId],
  219. AliYunKeySecret = Configuration[DefaultConfig.AliYunKeySecret],
  220. BasePath = Configuration[DefaultConfig.AliYunBasePath],
  221. CDNEndPoint = Configuration[DefaultConfig.AliYunPubEndPoint],
  222. };
  223. }
  224. if (serviceOption != null && serviceOption.AliYunConfig != null)
  225. {
  226. //阿里云
  227. services.AddSingleton<IAliYunPostFileSerivce>(x => new AliYunPostFileSerivce(serviceOption.AliYunConfig));
  228. }
  229. //excel
  230. services.AddSingleton<IExcelHelper, MSExcelHelper>();
  231. if (serviceOption != null && !string.IsNullOrEmpty(serviceOption.PBESConnection))
  232. {
  233. //pb
  234. //services.AddSingleton<IPerformanceDB>(x => new ESPerformanceDB(serviceOption.PBESConnection));
  235. }
  236. if (serviceOption != null && serviceOption.UseRabbit)
  237. {
  238. //Rebbit服务注册
  239. try
  240. {
  241. services.AddSingleton(x => RabbitHutch.CreateBus(Configuration[DefaultConfig.RabbitConnectionKey]));
  242. services.AddSingleton<IMQManager, MQManager>();
  243. }
  244. catch (Exception ex)
  245. {
  246. Log.Error("服务注册失败" + ex.Message);
  247. }
  248. }
  249. if (serviceOption != null)
  250. {
  251. //缓存
  252. switch (serviceOption.CacheType)
  253. {
  254. case DistributedCacheType.Memory:
  255. services.AddDistributedMemoryCache();
  256. break;
  257. case DistributedCacheType.Redis:
  258. services.AddDistributedRedisCache(x =>
  259. {
  260. x.Configuration = serviceOption.RedisConnection;
  261. x.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions
  262. {
  263. Password = "12345",
  264. };
  265. x.ConfigurationOptions.EndPoints.Add(serviceOption.RedisConnection);
  266. });
  267. break;
  268. }
  269. //缓存
  270. }
  271. //计费缓存
  272. if (serviceOption != null && serviceOption.UseFinanceChargesRedis)
  273. {
  274. services.AddSingleton(x => new RedisHelper(serviceOption.FinanceChargesRedisConnection, "FinanceChargesRedis", "r-wz9o30bx758o8jhbra:100Ep*dp", 2));
  275. }
  276. if (serviceOption != null && serviceOption.UseTrackingPUSHRedis)
  277. {
  278. services.AddSingleton(x => new RedisHelper(serviceOption.RedisConnection, "TrackingPUSHRedis", "r-wz9o30bx758o8jhbra:100Ep*dp", 3));
  279. }
  280. if (serviceOption != null && !string.IsNullOrEmpty(serviceOption.SqlServiceConnection))
  281. {
  282. //数据
  283. services.AddDB(serviceOption.SqlServiceConnection);
  284. AddDataService(services);
  285. }
  286. if (serviceOption != null && serviceOption.USEDBTransferAsMVC)
  287. {
  288. //事务
  289. services.ConfigAspectCore("Repository");
  290. services.AddMvcCore(x =>
  291. {
  292. x.Filters.Add<ApiExceptionAcitonFilterAttribute>();
  293. x.Filters.Add<ApiActionFilterAttribute>();
  294. });
  295. }
  296. if (serviceOption == null || !serviceOption.NoService)
  297. {
  298. try
  299. {
  300. var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
  301. var referencedAssemblies = System.IO.Directory.GetFiles(path, "XYY.Service.Standard.*.dll")
  302. .Where(x => !System.IO.Path.GetFileName(x).Equals("XYY.Service.Standard.RegionService.dll"))
  303. .Select(Assembly.LoadFrom).ToArray();
  304. var types = referencedAssemblies
  305. .SelectMany(a => a.DefinedTypes)
  306. .Select(type => type.AsType())
  307. .Where(x =>
  308. x.Name.EndsWith("Service")
  309. || x.Name.EndsWith("Api")
  310. ).ToArray();
  311. var implementTypes = types.Where(x => x.IsClass).ToArray();
  312. var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
  313. foreach (var implementType in implementTypes)
  314. {
  315. if (implementType.FullName == "XYY.Service.Standard.ChannelApi.Ultidel.Api")
  316. {
  317. }
  318. //var dd = interfaceTypes.Where(x => x.IsAssignableFrom(implementType));
  319. var allInterfaceTypes = interfaceTypes.Where(x => x.IsAssignableFrom(implementType));
  320. if (allInterfaceTypes?.Any() == true)
  321. {
  322. foreach (var interfaceType in allInterfaceTypes)
  323. {
  324. services.AddScoped(interfaceType, implementType);
  325. }
  326. }
  327. }
  328. }
  329. catch
  330. {
  331. }
  332. }
  333. //更改注入方式,注意需要取消订单的api,仅使用Repository 不用services,不能产生依赖关系
  334. //注入处理比较麻烦,尽可能让api纯净
  335. if (serviceOption == null || serviceOption.OpenChannelApi)
  336. {
  337. string[] regeionTypes = new string[]
  338. {
  339. "XYY.Data.Standard.dll",
  340. "XYY.Service.Standard.ChannelApi.dll",
  341. };
  342. string[] fullNames = new string[] { "XYY.Data.Standard.Order", "XYY.Data.Standard.Channel", "XYY.Service.Standard.ChannelApi.Frayun.Api", "XYY.Service.Standard.ChannelApi.SKPB.Api", "XYY.Service.Standard.ChannelApi.MIQ.Api", "XYY.Service.Standard.ChannelApi.Winit.LMAApi", "XYY.Service.Standard.ChannelApi.Winit.ISPApi", "XYY.Service.Standard.ChannelApi.Ultidel" };
  343. var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
  344. var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll")
  345. .Where(x => regeionTypes.Contains(System.IO.Path.GetFileName(x)))
  346. .Select(Assembly.LoadFrom).ToArray();
  347. var types = referencedAssemblies
  348. .SelectMany(a => a.DefinedTypes)
  349. .Select(type => type.AsType())
  350. .Where(x => fullNames.Any(p => x.FullName.Contains(p))
  351. || x.Name.Contains("IBaseApi")
  352. ).ToArray();
  353. var implementTypes = types.Where(x => x.IsClass).ToArray();
  354. var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
  355. foreach (var implementType in implementTypes)
  356. {
  357. var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
  358. if (interfaceType != null)
  359. services.AddSingleton(interfaceType, implementType);
  360. }
  361. }
  362. var fiddlerLog = new LoggerConfiguration()
  363. .MinimumLevel.Warning()
  364. .MinimumLevel.Override("LogginService", Serilog.Events.LogEventLevel.Warning)
  365. .Enrich.FromLogContext()
  366. .WriteTo.Seq("http://47.244.232.78:5341/")
  367. .WriteTo.Debug().CreateLogger();
  368. services.AddLogging(logging =>
  369. {
  370. logging.AddSerilog(fiddlerLog);
  371. });
  372. //注入访问监听
  373. if (serviceOption != null && serviceOption.UseFiddler)
  374. {
  375. services.AddSingleton<FiddlerClient>();
  376. }
  377. return services;
  378. }
  379. }
  380. }