猿问

正常关闭和当前正在处理的请求

首先,您应该重构您的代码以用 Person 替换 Age :


public class Person

{

    public string Name { get; set; }

    public string Age { get; set; }

}

然后,在你的程序中,你需要一个存放你的人的地方和一个显示结果的方法:


class Program

{


    public static Person Person { get; set; }


    static void Main(string[] args)

    {


        Person = new Person

        {

            Age = 10,

            Name = "Andrew"

        };


        TestOutput();

    }


    /// <summary>

    /// Method to show result

    /// </summary>

    static void TestOutput()

    {

        Console.WriteLine(Person.Name);

    }


}


慕码人2483693
浏览 178回答 2
2回答

守着星空守着你

我建议执行传入 CancellationToken 的 IWebHost.RunAsync 以便当 SIGINT 和 Ctrl+C 命令发送到您的应用程序时,您可以拦截和取消令牌,这将导致应用程序正常关闭。请参阅以下代码示例:public class Program{&nbsp; &nbsp; private static readonly CancellationTokenSource cts = new CancellationTokenSource();&nbsp; &nbsp; protected Program()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public static int Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.CancelKeyPress += OnExit;&nbsp; &nbsp; &nbsp; &nbsp; return RunHost(configuration).GetAwaiter().GetResult();&nbsp; &nbsp; }&nbsp; &nbsp; protected static void OnExit(object sender, ConsoleCancelEventArgs args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; cts.Cancel();&nbsp; &nbsp; }&nbsp; &nbsp; static async Task<int> RunHost()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; await new WebHostBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UseStartup<Startup>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .RunAsync(cts.Token);&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答