Startup.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Microsoft.AspNetCore.Authentication.Cookies;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.EntityFrameworkCore;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Hosting;
  9. using Microsoft.Extensions.WebEncoders;
  10. using ProductionLineMonitor.Application.Services;
  11. using ProductionLineMonitor.Application.Services.AdminService;
  12. using ProductionLineMonitor.Application.Services.CimService;
  13. using ProductionLineMonitor.Application.Services.FaultService;
  14. using ProductionLineMonitor.Application.Services.HomeService;
  15. using ProductionLineMonitor.Application.Services.LineService;
  16. using ProductionLineMonitor.Application.Services.OEEService;
  17. using ProductionLineMonitor.Core.IRepositories;
  18. using ProductionLineMonitor.Core.Services;
  19. using ProductionLineMonitor.EntityFramework;
  20. using ProductionLineMonitor.EntityFramework.Repositories;
  21. using ProductionLineMonitor.Web.Converter;
  22. using ProductionLineMonitor.Web.HostedServices;
  23. using System;
  24. using System.Text.Encodings.Web;
  25. using System.Text.Unicode;
  26. namespace ProductionLineMonitor.Web
  27. {
  28. public class Startup
  29. {
  30. public Startup(IConfiguration configuration)
  31. {
  32. Configuration = configuration;
  33. }
  34. public IConfiguration Configuration { get; }
  35. // This method gets called by the runtime. Use this method to add services to the container.
  36. public void ConfigureServices(IServiceCollection services)
  37. {
  38. services.AddControllers().AddJsonOptions(options =>
  39. {
  40. //设置时间格式
  41. options.JsonSerializerOptions.Converters.Add(new DateTimeConverter("yyyy-MM-dd HH:mm:ss"));
  42. });
  43. DBOptions.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
  44. services.AddDbContext<ProductionLineContext>(
  45. opt => opt.UseSqlite(DBOptions.ConnectionString,
  46. b => b.MigrationsAssembly("ProductionLineMonitor.Web")));
  47. services.Configure<WebEncoderOptions>(options =>
  48. options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All));
  49. services
  50. .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  51. .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, option =>
  52. {
  53. option.LoginPath = new PathString("/Account/Login");
  54. });
  55. // AutoMapper
  56. services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
  57. // Services
  58. services.AddScoped<IUnitOfWork, UnitOfWork>();
  59. services.AddScoped<IAdminService, AdminService>();
  60. services.AddScoped<IProductionLineCoreService, ProductionLineCoreService>();
  61. services.AddScoped<IRecipeService, RecipeService>();
  62. services.AddScoped<ICimService, CimService>();
  63. services.AddScoped<IReportFormService, ReportFormService>();
  64. services.AddScoped<IFaultService, FaultService>();
  65. services.AddScoped<IExternalService, ExternalService>();
  66. services.AddScoped<ILineService, LineService>();
  67. services.AddScoped<IHomeService, HomeService>();
  68. services.AddScoped<IOEEService, OEEService>();
  69. services.AddScoped<IExcelService, ExcelService>();
  70. // Dispatch service
  71. services.AddSingleton<IHostedService, DispatchService>();
  72. services.AddSingleton<IHostedService, ElectricEnergyMeterCheckService>();
  73. // Publish show views
  74. services.AddControllersWithViews();
  75. services.AddControllersWithViews().AddRazorRuntimeCompilation();
  76. }
  77. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  78. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  79. {
  80. if (env.IsDevelopment())
  81. {
  82. app.UseDeveloperExceptionPage();
  83. }
  84. app.UseStaticFiles();
  85. app.UseRouting();
  86. app.UseAuthentication();
  87. app.UseAuthorization();
  88. app.UseEndpoints(endpoints =>
  89. {
  90. endpoints.MapControllerRoute(
  91. name: "default",
  92. pattern: "{controller=Home}/{action=Index}/{id?}");
  93. });
  94. // Hsl Authorization
  95. var rev = HslCommunication.Authorization.SetAuthorizationCode("3e54799d-f65f-4be6-9e34-517ea9ee294c");
  96. if (rev == false)
  97. {
  98. throw new NotImplementedException("hsl 激活失败!");
  99. }
  100. // MQTT Client
  101. string ip = Configuration.GetSection("MqttOptions").GetValue<string>("IpAddress");
  102. int port = Configuration.GetSection("MqttOptions").GetValue<int>("Port");
  103. string clientId = Configuration.GetSection("MqttOptions").GetValue<string>("ClientId");
  104. app.UseHostMqtt(ip, port, clientId);
  105. // Safeguard Url
  106. Config.SafeguardUrl = Configuration.GetValue<string>("SafeguardUrl");
  107. }
  108. }
  109. }