using GZY.Quartz.MUI.Extensions; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using NLog.Extensions.Logging; using System.Reflection; using webapi_rumen_class_lib; using webapi_rumen_class_lib.SwaggerExtend; namespace webapi_rumen { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add services to the container. // builder.Services.AddControllers(); // 当前行是默认写法,API 返回的时间格式不是我们想要的:2024-06-02T22:58:06 // 下面的写法对 API 返回的时间格式做了设置:2024-06-02 22:58:06 builder.Services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }); #region 注释对跨域的配置,引入下方跨域设置的封装方法 //// 添加跨域策略 //builder.Services.AddCors(options => //{ // // 允许所有源、所有头、所有方法跨域 // options.AddPolicy("CorsPolicy",opt => opt.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); //}); #endregion // 引入跨域设置的封装方法 builder.Services.ConfigureCors(); // 引入数据库上下文配置 builder.Services.ConfigureMySqlContext(builder.Configuration); #region 注释掉对 Swagger 的配置,引入下方对 Swagger 的封装方法 //builder.Services.AddEndpointsApiExplorer(); //builder.Services.AddSwaggerGen(option => //{ // #region Swagger的注释的展示 // { // // 获取应用程序所在目录(项目文件夹下的bin\Debug\net6.0\中的exe文件所在目录) // string basePath = AppContext.BaseDirectory; // // 拼接上目录中保存注释的文件webapi_rumen.xml的文件名 // string xmlPath = Path.Combine(basePath, "webapi_rumen.xml"); // // 加载保存注释的文件 // option.IncludeXmlComments(xmlPath); // } // #endregion // #region Swagger的版本控制 // { // // 获取到所有的版本并遍历 // foreach (FieldInfo field in typeof(ApiVersionInfo).GetFields()) // { // // 配置Swagger的Doc文档 // option.SwaggerDoc(field.Name, new OpenApiInfo() // { // // 指定版本 // Title = $"{field.Name}:这里是 webapi 学习", // Version = field.Name, // Description = $"webapi {field.Name} 版本" // 描述 // }); // } // } // #endregion // #region Swagger的token传递 // { // option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() // { // Description = "请输入 Token,格式为 Bearer xxxxxxx(注意中间必须有空格)", // 描述 // Name = "Authorization", // 名字 // In = ParameterLocation.Header, // 指定将Token放到HTTP请求的头信息里面进行传递 // Type = SecuritySchemeType.ApiKey, // 类型是ApiKey // BearerFormat = "JWT", // "JWT" 代表 JSON Web Token,这是一种用于安全传输信息的开放标准 // Scheme = "Bearer" // 表示 API 要求客户端在发送请求时使用 Bearer 认证方案,即将访问令牌作为身份验证凭据的一部分,并在请求头部中使用 Authorization 字段来传递这个令牌。 // }); // // 添加安全要求 // option.AddSecurityRequirement(new OpenApiSecurityRequirement // { // { // new OpenApiSecurityScheme // { // Reference = new OpenApiReference() // { // Type = ReferenceType.SecurityScheme, // Id = "Bearer" // } // }, // new string[]{} // } // }); // } // #endregion //}); #endregion // 引入Swagger的封装方法 //SwaggerExtension.AddSwaggerExt(builder.Services); // 这种写法与其他代码不统一,注释掉,使用下一行的代码 builder.Services.AddSwaggerExt(); // 引用 log4net,指定配置文件的路径 // 下面的 AddLog4Net() 来自于之前安装的 Microsoft.Extensions.Logging.Log4Net.AspNetCore 包 //builder.Logging.AddLog4Net("LogConfig/log4net.config"); // 配置 Nlog,指定配置文件的领 builder.Logging.AddNLog("LogConfig/NLog.config"); // 添加定时任务调度Quartz builder.Services.AddQuartzUI(); // 引用本地调度任务访问 builder.Services.AddQuartzClassJobs(); // 添加 HttpClient 服务 builder.Services.AddHttpClient(); var app = builder.Build(); // 使用跨域策略 app.UseCors("CorsPolicy"); // CorsPolicy为跨域设置中指定的名称 // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { #region 注释掉对Swagger的配置,引入下方对 Swagger 的封装方法 //app.UseSwagger(); //app.UseSwaggerUI(option => //{ // // 获取到所有的版本并遍历 // foreach (FieldInfo field in typeof(ApiVersionInfo).GetFields()) // { // // 指定版本,每个版本会生成一个json文件 // option.SwaggerEndpoint($"/swagger/{field.Name}/swagger.json",$"{field.Name}"); // } //}); #endregion // 引入Swagger的封装方法 //SwaggerExtension.UseSwaggerExt(app); // 这种写法与其他代码不统一,注释掉,使用下一行的代码 app.UseSwaggerExt(); } app.UseAuthorization(); app.MapControllers(); // 使用定时任务调度Quartz app.UseQuartz(); app.Run(); } } }