using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.WebEncoders; using ProductionLineMonitor.Application.Services; using ProductionLineMonitor.Application.Services.AdminService; using ProductionLineMonitor.Application.Services.CimService; using ProductionLineMonitor.Application.Services.FaultService; using ProductionLineMonitor.Application.Services.HomeService; using ProductionLineMonitor.Application.Services.LineService; using ProductionLineMonitor.Application.Services.OEEService; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Services; using ProductionLineMonitor.EntityFramework; using ProductionLineMonitor.EntityFramework.Repositories; using ProductionLineMonitor.Web.Converter; using ProductionLineMonitor.Web.HostedServices; using System; using System.Text.Encodings.Web; using System.Text.Unicode; namespace ProductionLineMonitor.Web { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddJsonOptions(options => { //设置时间格式 options.JsonSerializerOptions.Converters.Add(new DateTimeConverter("yyyy-MM-dd HH:mm:ss")); }); DBOptions.ConnectionString = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext( opt => opt.UseSqlite(DBOptions.ConnectionString, b => b.MigrationsAssembly("ProductionLineMonitor.Web"))); services.Configure(options => options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All)); services .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, option => { option.LoginPath = new PathString("/Account/Login"); }); // AutoMapper services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); // Services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Dispatch service services.AddSingleton(); services.AddSingleton(); // Publish show views services.AddControllersWithViews(); services.AddControllersWithViews().AddRazorRuntimeCompilation(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); // Hsl Authorization var rev = HslCommunication.Authorization.SetAuthorizationCode("3e54799d-f65f-4be6-9e34-517ea9ee294c"); if (rev == false) { throw new NotImplementedException("hsl 激活失败!"); } // MQTT Client string ip = Configuration.GetSection("MqttOptions").GetValue("IpAddress"); int port = Configuration.GetSection("MqttOptions").GetValue("Port"); string clientId = Configuration.GetSection("MqttOptions").GetValue("ClientId"); app.UseHostMqtt(ip, port, clientId); // Safeguard Url Config.SafeguardUrl = Configuration.GetValue("SafeguardUrl"); } } }