You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.5 KiB
C#

11 months ago
using System.Diagnostics;
11 months ago
11 months ago
using HttpClientStudy.Core.Utilities;
11 months ago
9 months ago
using Microsoft.OpenApi.Models;
11 months ago
//启动WebApi程序
AppUtility.StartWebApiProject();
11 months ago
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();
9 months ago
//配置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的TokenBearer {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();
});
11 months ago
var app = builder.Build();
// Configure the HTTP request pipeline.
9 months ago
app.UseSwagger();
app.UseSwaggerUI(setup =>
11 months ago
{
9 months ago
setup.EnableDeepLinking();
setup.DisplayRequestDuration();
setup.ShowCommonExtensions();
setup.ShowExtensions();
setup.EnableFilter();
});
11 months ago
app.UseAuthorization();
app.MapControllers();
11 months ago
#region 退出时关闭WebAPI进程
/*
11 months ago
* 1退退
* 2退 ctl+c
* 3退 WebAPI退退
*/
11 months ago
11 months ago
// 获取 IHostApplicationLifetime 实例
11 months ago
var applicationLifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
11 months ago
// 注册应用程序停止事件关闭WebApi
applicationLifetime.ApplicationStopping.Register(() =>
11 months ago
{
11 months ago
//注意:在 Visual Studio 2022 预览版中执行时异常正式版中正常。应该是预案版的Bug
AppUtility.ExitWebApiProject();
11 months ago
});
applicationLifetime.ApplicationStopped.Register(() =>
11 months ago
{
11 months ago
Console.WriteLine("程序已停止");
});
11 months ago
#endregion
11 months ago
//默认Swagger页
app.Map("/", async context =>
{
context.Response.Redirect($"{context.Request.PathBase}/swagger/index.html");
await Task.CompletedTask;
});
11 months ago
app.Run();