コード例 #1
0
        /// <summary>
        /// Returns a Task that completes when shutdown is triggered via the given token, Ctrl+C or SIGTERM.
        /// </summary>
        /// <param name="host">The running <see cref="T:Microsoft.AspNetCore.Hosting.IWebHost" />.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        /// <param name="runSuccessCallback">启动成功的回调函数(Run the successful callback)</param>
        public static async Task WaitForShutdownAsync(this IWebHost host, CancellationToken token = default(CancellationToken), Action runSuccessCallback = null)
        {
            ManualResetEventSlim done = new ManualResetEventSlim(false);

            using (CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(token))
            {
                EWebHostExtensions.AttachCtrlcSigtermShutdown(cts, done, string.Empty);
                await host.WaitForTokenShutdownAsync(cts.Token, runSuccessCallback);

                done.Set();
            }
        }
コード例 #2
0
        /// <summary>
        /// Runs a web application and returns a Task that only completes when the token is triggered or shutdown is triggered.
        /// </summary>
        /// <param name="host">The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHost" /> to run.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        /// <param name="runSuccessCallback">启动成功的回调函数(Run the successful callback)</param>
        public static async Task RunAsync(this IWebHost host, CancellationToken token = default(CancellationToken), Action runSuccessCallback = null)
        {
            if (token.CanBeCanceled)
            {
                await host.RunAsync(token, (string)null, runSuccessCallback);
            }
            else
            {
                ManualResetEventSlim done = new ManualResetEventSlim(false);
                using (CancellationTokenSource cts = new CancellationTokenSource())
                {
                    EWebHostExtensions.AttachCtrlcSigtermShutdown(cts, done, host.Services.GetRequiredService <WebHostOptions>().SuppressStatusMessages ? string.Empty : "Application is shutting down...");

                    await host.RunAsync(
                        cts.Token,
                        "Application started. Press Ctrl+C to shut down.",
                        runSuccessCallback);

                    done.Set();
                }
            }
        }