```c#= using Chailease.AppMiddleware.API.Util; using Chailease.AppMiddleware.API; using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NLog; using NLog.Extensions.Logging; using NLog.Web; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using LogLevel = Microsoft.Extensions.Logging.LogLevel; using Microsoft.Extensions.DependencyInjection; using Chailease.AppMiddleware.API.ActionFilter; using Chailease.AppMiddleware.API.Extensions; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Newtonsoft.Json; using Microsoft.AspNetCore.Http; using Chailease.AppMiddleware.Service.Interface; using Chailease.Common.BuisnessLogicLayer; using Chailease.Common.DataAccessLayer; using Chailease.AppMiddleware.Service; var config = new ConfigurationBuilder() .SetBasePath(System.IO.Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT")}.json", optional: true) .AddJsonFile("nlogsettings.json", optional: true, reloadOnChange: true) .Build(); var logger = NLogBuilder.ConfigureNLog(new NLogLoggingConfiguration(config.GetSection("NLog"))) .GetCurrentClassLogger(); logger.Debug("init main"); try { var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var configuration = builder.Configuration; int intProcessorCount = Environment.ProcessorCount; ThreadPool.SetMaxThreads(100 * intProcessorCount, 100 * intProcessorCount); ThreadPool.SetMinThreads(50 * intProcessorCount, 50 * intProcessorCount); builder.Services.AddControllers(options => { //不須使用MS SQL,故不須加上判斷Azure SQL MI錯誤訊息判斷 //options.Filters.Add<DBMaintenanceFilter>(); }).AddFluentValidation().AddDateTimeFormat(); builder.Services.AddTransient<IValidatorInterceptor, ValidatorInterceptor>(); builder.Services.AddHttpClientRequestTraceHandler(); builder.Services.AddHostedService<ApplicationLifetimeManager>(); builder.Services.AddSwaggerServices(); builder.Services.AddPolicyRegistry(); // Entity Framework DB註冊,未使用,改使用dapper //builder.Services.AddDatabaseServices(configuration); // Redis註冊,未使用 //builder.Services.AddRedisServices(configuration); // DI註冊 builder.Services.AddFeatureServices(); builder.Services.AddCorsServices(); builder.Services.AddJWTServices(); builder.Services.AddHttpClientServices(configuration); builder.Services.AddHealthChecksServices(configuration); builder.Services.AddOpenTracingServices(configuration); builder.Services.AddHttpContextAccessor(); //NLog Setup builder.Logging.ClearProviders(); builder.Logging.SetMinimumLevel(LogLevel.Trace); builder.Host.UseNLog(); builder.Services.AddSingleton<CacheService>(); var app = builder.Build(); // Bind Trace Record to Httpcontext app.UseTraceMiddleware(); // app.UseResponseCompression(); // Handle All exception here app.UseExceptionMiddleware(); // use swagger app.UseSwagger(); app.UseSwaggerUI(c => { var prefix = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : ".."; c.SwaggerEndpoint($"{prefix}/swagger/v1/swagger.json", "Chailease.AppMiddleware.API v1"); }); // Health check app.UseHealthChecks("/health", new HealthCheckOptions() { ResponseWriter = async (context, report) => { var json = JsonConvert.SerializeObject(report); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(json); }, }); app.UseRouting(); app.UseHeaderPropagation(); app.UseCors(CorsExtensions.AllowAny); app.UseAuthentication(); app.UseAuthorization(); // Grab UserNo from Http Header app.UseByPassAuthMiddleware(); #pragma warning disable ASP0014 app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); #pragma warning disable ASP0014 app.Run(); } catch (Exception exception) { //NLog: catch setup errors logger.Error(exception, "Stopped program because of exception"); throw; } finally { // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) NLog.LogManager.Shutdown(); } ```