Startup.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.HttpsPolicy;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.Hosting;
  11. using Newtonsoft.Json.Serialization;
  12. using Quartz;
  13. using Quartz.Impl;
  14. using WCS.Data;
  15. using WCS.Workflow;
  16. namespace WCS.Service
  17. {
  18. public class Startup
  19. {
  20. public Startup(IConfiguration configuration)
  21. {
  22. Configuration = configuration;
  23. SugarBase.DBConnectionString = Configuration["SQLServer"];
  24. AppSettingsHelper.AppSettings = Configuration.GetSection("AppSettings").Get<AppSettings>();
  25. }
  26. public IConfiguration Configuration { get; }
  27. // This method gets called by the runtime. Use this method to add services to the container.
  28. public void ConfigureServices(IServiceCollection services)
  29. {
  30. services.AddControllersWithViews();
  31. Log4netHelper.InitLog4net("NETCoreRepository");
  32. services.AddMvc().AddNewtonsoftJson(options =>
  33. {
  34. options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //去除首字母小写
  35. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm"; //格式化时间
  36. });
  37. //services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
  38. #region 注入 Quartz调度类
  39. services.AddSingleton<QuartzStartup>();
  40. //注册ISchedulerFactory的实例
  41. services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
  42. #endregion;
  43. }
  44. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  45. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
  46. {
  47. if (env.IsDevelopment())
  48. {
  49. app.UseDeveloperExceptionPage();
  50. }
  51. else
  52. {
  53. app.UseExceptionHandler("/Home/Error");
  54. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  55. app.UseHsts();
  56. }
  57. Log4netHelper.Logger_Info.InfoFormat("服务重启");
  58. InitWorkflow.InitData();
  59. //获取前面注入的Quartz调度类
  60. var quartz = app.ApplicationServices.GetRequiredService<QuartzStartup>();
  61. appLifetime.ApplicationStarted.Register(() =>
  62. {
  63. quartz.Start().Wait();
  64. });
  65. appLifetime.ApplicationStopped.Register(() =>
  66. {
  67. quartz.Stop();
  68. });
  69. app.UseHttpsRedirection();
  70. app.UseStaticFiles();
  71. app.UseRouting();
  72. app.UseAuthorization();
  73. app.UseEndpoints(endpoints =>
  74. {
  75. endpoints.MapControllerRoute(
  76. name: "default",
  77. pattern: "{controller=Home}/{action=Index}/{id?}");
  78. });
  79. }
  80. }
  81. }