|
|
using System.Diagnostics;
|
|
|
|
|
|
using HttpClientStudy.Core.Utilities;
|
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
|
//启动WebApi程序
|
|
|
AppUtility.StartWebApiProject();
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
//配置Swagger
|
|
|
builder.Services.AddSwaggerGen(setup =>
|
|
|
{
|
|
|
#region 定义Swagger文档
|
|
|
//name参数即为SwaggerUI中SwaggerEndpoint方法参数中的{documentName}
|
|
|
//两者必须保持一致,否则异常
|
|
|
setup.SwaggerDoc(name: "v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "HttpClient学习", Version = "第1版" });
|
|
|
#endregion
|
|
|
|
|
|
#region 包含xml注释
|
|
|
var xmlCommentFiles = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "HttpClientStudy.*.xml", System.IO.SearchOption.TopDirectoryOnly);
|
|
|
foreach (var xmlFile in xmlCommentFiles)
|
|
|
{
|
|
|
//includeControllerXmlComments参数:是否启用控制器上的xml注释
|
|
|
setup.IncludeXmlComments(filePath: xmlFile, includeControllerXmlComments: true);
|
|
|
|
|
|
setup.UseInlineDefinitionsForEnums();
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
/*
|
|
|
#region 放置接口Auth授权按钮
|
|
|
|
|
|
setup.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
{
|
|
|
Description = "请输入带有Bearer的Token:Bearer {Token}",
|
|
|
|
|
|
//jwt默认的参数名称
|
|
|
Name = "Authorization",
|
|
|
|
|
|
//jwt默认存放 Authorization 信息的位置:此处为请求头中
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
|
//验证类型:此处使用Api Key
|
|
|
Type = SecuritySchemeType.ApiKey
|
|
|
});
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 指定方案应用范围
|
|
|
setup.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
{
|
|
|
{
|
|
|
new OpenApiSecurityScheme
|
|
|
{
|
|
|
Reference = new OpenApiReference
|
|
|
{
|
|
|
Id = "Bearer",
|
|
|
Type = ReferenceType.SecurityScheme
|
|
|
}
|
|
|
},
|
|
|
new List<string>()
|
|
|
}
|
|
|
});
|
|
|
#endregion
|
|
|
*/
|
|
|
|
|
|
//启用数据注解
|
|
|
setup.EnableAnnotations();
|
|
|
});
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
app.UseSwagger();
|
|
|
app.UseSwaggerUI(setup =>
|
|
|
{
|
|
|
setup.EnableDeepLinking();
|
|
|
setup.DisplayRequestDuration();
|
|
|
setup.ShowCommonExtensions();
|
|
|
setup.ShowExtensions();
|
|
|
setup.EnableFilter();
|
|
|
});
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
#region 退出时,关闭WebAPI进程
|
|
|
/*
|
|
|
* 1、退出执行机制,本身不太可靠,只有正常退出时才执行。
|
|
|
* 2、正常退出,比如 ctl+c 操作
|
|
|
* 3、非正常退出:比如 程序崩溃、内存泄漏、关闭进程,特别是调试WebAPI时,如果启动时打开了浏览器,则关闭浏览器时退出属于异常退出。
|
|
|
*/
|
|
|
|
|
|
// 获取 IHostApplicationLifetime 实例
|
|
|
var applicationLifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
|
|
|
|
|
|
// 注册应用程序停止事件:关闭WebApi
|
|
|
applicationLifetime.ApplicationStopping.Register(() =>
|
|
|
{
|
|
|
//注意:在 Visual Studio 2022 预览版中,执行时异常;正式版中正常。应该是预案版的Bug
|
|
|
AppUtility.ExitWebApiProject();
|
|
|
});
|
|
|
|
|
|
applicationLifetime.ApplicationStopped.Register(() =>
|
|
|
{
|
|
|
Console.WriteLine("程序已停止");
|
|
|
});
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
//默认Swagger页
|
|
|
app.Map("/", async context =>
|
|
|
{
|
|
|
context.Response.Redirect($"{context.Request.PathBase}/swagger/index.html");
|
|
|
|
|
|
await Task.CompletedTask;
|
|
|
});
|
|
|
|
|
|
app.Run();
|