Files
Web_HotelServices_Prod/WebUI/Startup.cs

211 lines
8.5 KiB
C#
Raw Normal View History

2025-11-26 11:18:26 +08:00
using COMMON;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Models;
using Models.Models.DEBUGGER_DB;
using Models.Models.Engineering;
using Models.Models.LOGDB;
using Quartz;
using Quartz.Impl;
using System.IO;
using System.IO.Compression;
using System.Linq;
using WebUI.LIB;
using static System.Net.Mime.MediaTypeNames;
namespace WebUI
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public class Startup
{
/// <summary>
/// sql <20><><EFBFBD><EFBFBD>
/// </summary>
// <20><><EFBFBD>Ӿ<EFBFBD>̬ILoggerFactory <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾefִ<66>е<EFBFBD>sql
public static readonly ILoggerFactory efLogger = LoggerFactory.Create(builder =>
{
builder.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
});
/// <summary>
/// <20>вι<D0B2><CEB9><EFBFBD>
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// <20><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
/// </summary>
/// <param name="services"></param>
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
Configuration.GetSection("ConfigEntity").Bind(ConfigEntity.Instance);
Configuration.GetSection("JwtConst").Bind(JwtConst.Instance);
services.AddResponseCompression();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddDbContext<DEBUGGER_DB>(opt =>
opt.UseSqlServer(ConfigEntity.Instance.DEBUGGER_DB,
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
if (ConfigEntity.Instance.DBTYPE == 1)
{
services.AddDbContext<HotelServiceContext>(opt =>
opt.UseSqlServer(ConfigEntity.Instance.Connection,
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
services.AddDbContext<LOG_DBContext>(opt =>
opt.UseSqlServer(ConfigEntity.Instance.Connection_log,
// <20><><EFBFBD>Ӿ<EFBFBD>̬ILoggerFactory <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾefִ<66>е<EFBFBD>sql
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
services.AddDbContext<Engineering_Management>(opt =>
opt.UseSqlServer(ConfigEntity.Instance.Engineering_DB,
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
}
if (ConfigEntity.Instance.DBTYPE == 2)
{
services.AddDbContext<HotelServiceContext>(opt =>
opt.UseMySQL(ConfigEntity.Instance.Connection,
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
services.AddDbContext<LOG_DBContext>(opt =>
opt.UseMySQL(ConfigEntity.Instance.Connection_log,
// <20><><EFBFBD>Ӿ<EFBFBD>̬ILoggerFactory <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾefִ<66>е<EFBFBD>sql
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
services.AddDbContext<Engineering_Management>(opt =>
opt.UseMySQL(ConfigEntity.Instance.Engineering_DB,
b => b.MigrationsAssembly("WebUI")).UseLoggerFactory(efLogger));
}
// <20><> newjson
services.AddControllersWithViews().AddJsonOptions(opt =>
{
//ԭ<><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><>ϻ<EFBFBD><CFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸСд
opt.JsonSerializerOptions.PropertyNamingPolicy = null;
// <20><>ʽ<EFBFBD><CABD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
opt.JsonSerializerOptions.Converters.Add(new DateTimeStr());
}).AddRazorRuntimeCompilation();
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" });
});
//<2F><><EFBFBD><EFBFBD>BrotliCompression ѹ<><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Optimal(<28><><EFBFBD>ѵ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ʽ,<2C>ķѵ<C4B7>ʱ<EFBFBD><CAB1><EFBFBD>ϳ<EFBFBD>) Fastest(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ʽ) NoCompression(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9>)
services.Configure<BrotliCompressionProviderOptions>(config =>
{
config.Level = CompressionLevel.Fastest;
});
//<2F><><EFBFBD><EFBFBD>Gzip ѹ<><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Optimal(<28><><EFBFBD>ѵ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ʽ,<2C>ķѵ<C4B7>ʱ<EFBFBD><CAB1><EFBFBD>ϳ<EFBFBD>) Fastest(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>ʽ) NoCompression(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9>)
services.Configure<GzipCompressionProviderOptions>(config =>
{
config.Level = CompressionLevel.Fastest;
});
}
/// <summary>
/// <20>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
/// <param name="applicationLeftTime"></param>
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLeftTime)
{
//<2F><><EFBFBD><EFBFBD>jwt<77><74>֤
app.UseAuthentication();
if (env.IsDevelopment())
{
var developerExceptionPageOptions = new DeveloperExceptionPageOptions();
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
developerExceptionPageOptions.SourceCodeLineCount = 10;
app.UseDeveloperExceptionPage();
}
else
{
// <20><><EFBFBD><EFBFBD>ҳ
app.UseExceptionHandler(exceptionHandlerApp =>
{
exceptionHandlerApp.Run(async context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = Text.Plain;
await context.Response.WriteAsync("An exception was thrown.");
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
{
await context.Response.WriteAsync(" The file was not found.");
}
if (exceptionHandlerPathFeature?.Path == "/")
{
await context.Response.WriteAsync(" Page: Home.");
}
});
});
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
//app.UseHsts();
}
//app.UseHttpsRedirection();
// <20><><EFBFBD><EFBFBD> UseStaticFiles ֮ǰ <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD>
// app.UseMiddleware<BadLink>();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>м<EFBFBD><D0BC><EFBFBD>֮ǰ<D6AE><C7B0><EFBFBD><EFBFBD>
app.UseResponseCompression();
applicationLeftTime.ApplicationStopped.Register(() => {
LogHelp.Error("ApplicationStopped...");
TcpServer.Tcp.Close();
});
}
}
}