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.
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
using HttpClientStudy.Core.Utilities;
|
|
|
|
|
|
|
|
|
|
//启动WebApi程序
|
|
|
|
|
StartupUtility.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();
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
StartupUtility.ExitWebApiProject();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
applicationLifetime.ApplicationStopped.Register(() =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("程序已停止");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
app.Run();
|