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.

63 lines
1.6 KiB
C#

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