ProcurementHostModule.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using Procurement.EntityFrameworkCore;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Cors;
  5. using Microsoft.AspNetCore.DataProtection;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.OpenApi.Models;
  11. using StackExchange.Redis;
  12. using System;
  13. using System.IO;
  14. using System.Linq;
  15. using Volo.Abp;
  16. using Volo.Abp.AspNetCore.MultiTenancy;
  17. using Volo.Abp.AspNetCore.Mvc;
  18. using Volo.Abp.AspNetCore.Serilog;
  19. using Volo.Abp.Autofac;
  20. using Volo.Abp.Caching;
  21. using Volo.Abp.Data;
  22. using Volo.Abp.EntityFrameworkCore.MySQL;
  23. using Volo.Abp.Localization;
  24. using Volo.Abp.Modularity;
  25. using Volo.Abp.MultiTenancy;
  26. using Volo.Abp.Threading;
  27. using Volo.Abp.VirtualFileSystem;
  28. using Procurement.Quartz;
  29. using Quartz;
  30. namespace Procurement
  31. {
  32. [DependsOn(
  33. typeof(AbpAutofacModule),
  34. typeof(AbpEntityFrameworkCoreMySQLModule),
  35. typeof(ProcurementApplicationModule),
  36. typeof(ProcurementEntityFrameworkCoreModule),
  37. typeof(AbpAspNetCoreMultiTenancyModule),
  38. typeof(AbpAspNetCoreSerilogModule),
  39. typeof(AbpAspNetCoreMvcModule)
  40. )]
  41. public class ProcurementHostModule : AbpModule
  42. {
  43. private const string DefaultCorsPolicyName = "Default";
  44. public override void ConfigureServices(ServiceConfigurationContext context)
  45. {
  46. var configuration = context.Services.GetConfiguration();
  47. var hostingEnvironment = context.Services.GetHostingEnvironment();
  48. ConfigureConventionalControllers();
  49. ConfigureMultiTenancy();
  50. ConfigureAuthentication(context, configuration);
  51. ConfigureLocalization();
  52. ConfigureCache(configuration);
  53. ConfigureVirtualFileSystem(context);
  54. ConfigureCors(context, configuration);
  55. //ConfigureRedis(context, configuration, hostingEnvironment);
  56. ConfigureSwaggerServices(context);
  57. ConfigureQuartz(context, configuration);
  58. }
  59. private void ConfigureConventionalControllers()
  60. {
  61. Configure<AbpAspNetCoreMvcOptions>(options =>
  62. {
  63. options.ConventionalControllers.Create(typeof(ProcurementApplicationModule).Assembly, opts =>
  64. {
  65. opts.RootPath = "procurement";
  66. });
  67. });
  68. }
  69. private void ConfigureMultiTenancy()
  70. {
  71. Configure<AbpMultiTenancyOptions>(options =>
  72. {
  73. options.IsEnabled = true;
  74. });
  75. }
  76. private void ConfigureCache(IConfiguration configuration)
  77. {
  78. Configure<AbpDistributedCacheOptions>(options =>
  79. {
  80. options.KeyPrefix = "Procurement:";
  81. });
  82. }
  83. private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
  84. {
  85. context.Services.AddCors(options =>
  86. {
  87. options.AddPolicy(DefaultCorsPolicyName, builder =>
  88. {
  89. builder
  90. .WithOrigins(
  91. configuration["App:CorsOrigins"]
  92. .Split(",", StringSplitOptions.RemoveEmptyEntries)
  93. .Select(o => o.RemovePostFix("/"))
  94. .ToArray()
  95. )
  96. .WithAbpExposedHeaders()
  97. .SetIsOriginAllowedToAllowWildcardSubdomains()
  98. .AllowAnyHeader()
  99. .AllowAnyMethod()
  100. .AllowCredentials();
  101. });
  102. });
  103. }
  104. private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
  105. {
  106. var hostingEnvironment = context.Services.GetHostingEnvironment();
  107. if (hostingEnvironment.IsDevelopment())
  108. {
  109. Configure<AbpVirtualFileSystemOptions>(options =>
  110. {
  111. options.FileSets.ReplaceEmbeddedByPhysical<ProcurementDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Procurement.Domain"));
  112. options.FileSets.ReplaceEmbeddedByPhysical<ProcurementApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Procurement.Application.Contracts"));
  113. options.FileSets.ReplaceEmbeddedByPhysical<ProcurementApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Procurement.Application"));
  114. });
  115. }
  116. }
  117. private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
  118. {
  119. context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  120. .AddJwtBearer(options =>
  121. {
  122. options.Authority = configuration["AuthServer:Authority"];
  123. options.RequireHttpsMetadata = false;
  124. options.Audience = "BusinessService";
  125. });
  126. }
  127. private static void ConfigureSwaggerServices(ServiceConfigurationContext context)
  128. {
  129. context.Services.AddSwaggerGen(
  130. options =>
  131. {
  132. options.SwaggerDoc("v1", new OpenApiInfo { Title = "Procurement Service API", Version = "v1" });
  133. options.DocInclusionPredicate((docName, description) => true);
  134. });
  135. }
  136. private void ConfigureQuartz(ServiceConfigurationContext context, IConfiguration configuration)
  137. {
  138. //程序启动执行一次日志分表检查,可以自己初始化避免需要部署脚本
  139. LogHostedService logHostedService = new LogHostedService();
  140. logHostedService.LogInstall();
  141. context.Services.AddQuartz(q =>
  142. {
  143. q.UseMicrosoftDependencyInjectionScopedJobFactory();
  144. var NLogJobKey = new JobKey("NLogJob");
  145. q.AddJob<NLogJob>(opts => opts.WithIdentity(NLogJobKey));
  146. q.AddTrigger(opts => opts
  147. .ForJob(NLogJobKey)
  148. .WithIdentity("DemoJob-trigger")
  149. .WithCronSchedule("0 0 0 * * ?")
  150. .WithDescription("定时创建NLog日志按月分表"));
  151. });
  152. context.Services.AddQuartzServer(options =>
  153. {
  154. // when shutting down we want jobs to complete gracefully
  155. options.WaitForJobsToComplete = true;
  156. });
  157. //context.Services.AddQuartzHostedService(options =>
  158. //{
  159. // // when shutting down we want jobs to complete gracefully
  160. // options.WaitForJobsToComplete = true;
  161. //});
  162. }
  163. private void ConfigureLocalization()
  164. {
  165. Configure<AbpLocalizationOptions>(options =>
  166. {
  167. options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština"));
  168. options.Languages.Add(new LanguageInfo("en", "en", "English"));
  169. options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português"));
  170. options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe"));
  171. options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
  172. options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文"));
  173. });
  174. }
  175. private void ConfigureRedis(
  176. ServiceConfigurationContext context,
  177. IConfiguration configuration,
  178. IWebHostEnvironment hostingEnvironment)
  179. {
  180. context.Services.AddStackExchangeRedisCache(options =>
  181. {
  182. options.Configuration = configuration["Redis:Configuration"];
  183. });
  184. if (!hostingEnvironment.IsDevelopment())
  185. {
  186. var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
  187. context.Services
  188. .AddDataProtection()
  189. .PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");
  190. }
  191. }
  192. public override void OnApplicationInitialization(ApplicationInitializationContext context)
  193. {
  194. var app = context.GetApplicationBuilder();
  195. app.UseCorrelationId();
  196. app.UseStaticFiles();
  197. app.UseRouting();
  198. app.UseCors(DefaultCorsPolicyName);
  199. app.UseAuthentication();
  200. app.UseAbpClaimsMap();
  201. app.UseMultiTenancy();
  202. app.UseAbpRequestLocalization();
  203. app.UseSwagger();
  204. app.UseSwaggerUI(options =>
  205. {
  206. options.SwaggerEndpoint("/swagger/v1/swagger.json", "Procurement Service API");
  207. });
  208. app.UseAuditing();
  209. app.UseAbpSerilogEnrichers();
  210. app.UseConfiguredEndpoints();
  211. AsyncHelper.RunSync(async () =>
  212. {
  213. using (var scope = context.ServiceProvider.CreateScope())
  214. {
  215. await scope.ServiceProvider
  216. .GetRequiredService<IDataSeeder>()
  217. .SeedAsync();
  218. }
  219. });
  220. }
  221. }
  222. }