DigitalManufacturingHostModule.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using DigitalManufacturing.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 DigitalManufacturing.Quartz;
  29. using Quartz;
  30. namespace DigitalManufacturing
  31. {
  32. [DependsOn(
  33. typeof(AbpAutofacModule),
  34. typeof(AbpEntityFrameworkCoreMySQLModule),
  35. typeof(DigitalManufacturingApplicationModule),
  36. typeof(DigitalManufacturingEntityFrameworkCoreModule),
  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 ConfigureQuartz(ServiceConfigurationContext context, IConfiguration configuration)
  60. {
  61. //程序启动执行一次日志分表检查,可以自己初始化避免需要部署脚本
  62. LogHostedService logHostedService = new LogHostedService();
  63. logHostedService.LogInstall();
  64. context.Services.AddQuartz(q =>
  65. {
  66. q.UseMicrosoftDependencyInjectionScopedJobFactory();
  67. var NLogJobKey = new JobKey("NLogJob");
  68. q.AddJob<NLogJob>(opts => opts.WithIdentity(NLogJobKey));
  69. q.AddTrigger(opts => opts
  70. .ForJob(NLogJobKey)
  71. .WithIdentity("DemoJob-trigger")
  72. .WithCronSchedule("0 0 0 * * ?")
  73. .WithDescription("定时创建NLog日志按月分表"));
  74. });
  75. context.Services.AddQuartzServer(options =>
  76. {
  77. // when shutting down we want jobs to complete gracefully
  78. options.WaitForJobsToComplete = true;
  79. });
  80. //context.Services.AddQuartzHostedService(options =>
  81. //{
  82. // // when shutting down we want jobs to complete gracefully
  83. // options.WaitForJobsToComplete = true;
  84. //});
  85. }
  86. private void ConfigureConventionalControllers()
  87. {
  88. Configure<AbpAspNetCoreMvcOptions>(options =>
  89. {
  90. options.ConventionalControllers.Create(typeof(DigitalManufacturingApplicationModule).Assembly, opts =>
  91. {
  92. opts.RootPath = "digitalmanufacturing";
  93. });
  94. });
  95. }
  96. private void ConfigureMultiTenancy()
  97. {
  98. Configure<AbpMultiTenancyOptions>(options =>
  99. {
  100. options.IsEnabled = true;
  101. });
  102. }
  103. private void ConfigureCache(IConfiguration configuration)
  104. {
  105. Configure<AbpDistributedCacheOptions>(options =>
  106. {
  107. options.KeyPrefix = "DigitalManufacturing:";
  108. });
  109. }
  110. private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
  111. {
  112. context.Services.AddCors(options =>
  113. {
  114. options.AddPolicy(DefaultCorsPolicyName, builder =>
  115. {
  116. builder
  117. .WithOrigins(
  118. configuration["App:CorsOrigins"]
  119. .Split(",", StringSplitOptions.RemoveEmptyEntries)
  120. .Select(o => o.RemovePostFix("/"))
  121. .ToArray()
  122. )
  123. .WithAbpExposedHeaders()
  124. .SetIsOriginAllowedToAllowWildcardSubdomains()
  125. .AllowAnyHeader()
  126. .AllowAnyMethod()
  127. .AllowCredentials();
  128. });
  129. });
  130. }
  131. private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
  132. {
  133. var hostingEnvironment = context.Services.GetHostingEnvironment();
  134. if (hostingEnvironment.IsDevelopment())
  135. {
  136. Configure<AbpVirtualFileSystemOptions>(options =>
  137. {
  138. options.FileSets.ReplaceEmbeddedByPhysical<DigitalManufacturingDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}DigitalManufacturing.Domain"));
  139. options.FileSets.ReplaceEmbeddedByPhysical<DigitalManufacturingApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}DigitalManufacturing.Application.Contracts"));
  140. options.FileSets.ReplaceEmbeddedByPhysical<DigitalManufacturingApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}DigitalManufacturing.Application"));
  141. });
  142. }
  143. }
  144. private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
  145. {
  146. context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  147. .AddJwtBearer(options =>
  148. {
  149. options.Authority = configuration["AuthServer:Authority"];
  150. options.RequireHttpsMetadata = false;
  151. options.Audience = "BusinessService";
  152. });
  153. }
  154. private static void ConfigureSwaggerServices(ServiceConfigurationContext context)
  155. {
  156. context.Services.AddSwaggerGen(
  157. options =>
  158. {
  159. options.SwaggerDoc("v1", new OpenApiInfo { Title = "DigitalManufacturing Service API", Version = "v1" });
  160. options.DocInclusionPredicate((docName, description) => 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", "DigitalManufacturing 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. }