Windows , Linux ,Docker 都有效
Nuget
Microsoft.Extensions.Hosting 5.0.0
代码
方案1
using System;
using Microsoft.Extensions.Hosting;
namespace Demo
{
internal static class Program
{
public static void Main(string[] args)
{
// some code...
// ctrl + c
new HostBuilder().UseConsoleLifetime().Build().Run();
}
}
}
方案2
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace Demo
{
internal static class Program
{
public async static Task Main(string[] args)
{
// some code ...
// 监听 Ctrl + C
await new HostBuilder().RunConsoleAsync();
}
}
}
方案3
using System;
using Microsoft.Extensions.Hosting;
namespace Demo
{
internal static class Program
{
public static void Main(string[] args)
{
// some code ...
// Ctrl + c
Host.CreateDefaultBuilder().UseConsoleLifetime().Build().Run();
}
}
}