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.

55 lines
1.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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();