예제 #1
0
        private static async Task RunAsync(this IWebHost host, CancellationToken token, string shutdownMessage)
        {
            using (host)
            {
                await host.StartAsync(token);

                var hostingEnvironment = host.Services.GetService <IHostingEnvironment>();
                var options            = host.Services.GetRequiredService <WebHostOptions>();

                if (!options.SuppressStatusMessages)
                {
                    Console.WriteLine($"Hosting environment: {hostingEnvironment.EnvironmentName}");
                    Console.WriteLine($"Content root path: {hostingEnvironment.ContentRootPath}");


                    var serverAddresses = host.ServerFeatures.Get <IServerAddressesFeature>()?.Addresses;
                    if (serverAddresses != null)
                    {
                        foreach (var address in serverAddresses)
                        {
                            Console.WriteLine($"Now listening on: {address}");
                        }
                    }

                    if (!string.IsNullOrEmpty(shutdownMessage))
                    {
                        Console.WriteLine(shutdownMessage);
                    }
                }

                await host.WaitForTokenShutdownAsync(token);
            }
        }
예제 #2
0
        private static async Task RunAsync(this IWebHost host, ILogger logger, CancellationToken token, string shutdownMessage)
        {
            using (host) {
                await host.StartAsync(token);

                var hostingEnvironment = host.Services.GetService <IHostingEnvironment>();

                logger.LogInformation("Hosting environment: {EnvironmentName}", hostingEnvironment.EnvironmentName);
                logger.LogInformation("Content root path: {ContentRootPath}", hostingEnvironment.ContentRootPath);

                var serverAddresses = host.ServerFeatures.Get <IServerAddressesFeature>()?.Addresses;
                if (serverAddresses != null)
                {
                    foreach (var address in serverAddresses)
                    {
                        logger.LogInformation("Now listening on: {Address}", address);
                    }
                }

                if (!string.IsNullOrEmpty(shutdownMessage))
                {
                    logger.LogInformation(shutdownMessage);
                }

                await host.WaitForTokenShutdownAsync(token).AnyContext();
            }
        }
        private static async Task RunAsync(
            this IWebHost host,
            CancellationToken token,
            string shutdownMessage,
            Action runSuccessCallback)
        {
            using (host)
            {
                await host.StartAsync(token);

                IHostingEnvironment service = host.Services.GetService <IHostingEnvironment>();
                host.Services.GetService <IApplicationLifetime>();
                if (!host.Services.GetRequiredService <WebHostOptions>().SuppressStatusMessages)
                {
                    Console.WriteLine(string.Format("Hosting environment: {0}", (object)service.EnvironmentName));
                    Console.WriteLine(string.Format("Content root path: {0}", (object)service.ContentRootPath));
                    ICollection <string> addresses = host.ServerFeatures.Get <IServerAddressesFeature>()?.Addresses;
                    if (addresses != null)
                    {
                        foreach (object obj in (IEnumerable <string>)addresses)
                        {
                            Console.WriteLine(string.Format("Now listening on: {0}", obj));
                        }
                    }
                    if (!string.IsNullOrEmpty(shutdownMessage))
                    {
                        Console.WriteLine(shutdownMessage);
                    }
                }

                await host.WaitForTokenShutdownAsync(token, runSuccessCallback);
            }
        }
예제 #4
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="IWebHost"/>.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task WaitForShutdownAsync(this IWebHost host, CancellationToken token = default(CancellationToken))
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token))
            {
                await host.WaitForTokenShutdownAsync(cts.Token);

                done.Set();
            }
        }
        /// <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();
            }
        }
예제 #6
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="IWebHost"/>.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task WaitForShutdownAsync(this IWebHost host, CancellationToken token = default(CancellationToken))
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token))
            {
                AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: string.Empty);

                await host.WaitForTokenShutdownAsync(cts.Token);

                done.Set();
            }
        }
예제 #7
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="IWebHost"/>.</param>
        /// <param name="logger">The logger to log status messages to.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task WaitForShutdownAsync(this IWebHost host, ILogger logger, CancellationToken token = default)
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token)) {
                AttachCtrlcSigtermShutdown(cts, done, logger, shutdownMessage: String.Empty);

                try {
                    await host.WaitForTokenShutdownAsync(cts.Token).AnyContext();
                } finally {
                    done.Set();
                }
            }
        }
예제 #8
0
        private static async Task RunAsync(this IWebHost host, CancellationToken token, string?startupMessage)
        {
            try
            {
                await host.StartAsync(token);

                var hostingEnvironment = host.Services.GetService <IHostEnvironment>();
                var options            = host.Services.GetRequiredService <WebHostOptions>();

                if (!options.SuppressStatusMessages)
                {
                    Console.WriteLine($"Hosting environment: {hostingEnvironment?.EnvironmentName}");
                    Console.WriteLine($"Content root path: {hostingEnvironment?.ContentRootPath}");


                    var serverAddresses = host.ServerFeatures.Get <IServerAddressesFeature>()?.Addresses;
                    if (serverAddresses != null)
                    {
                        foreach (var address in serverAddresses)
                        {
                            Console.WriteLine($"Now listening on: {address}");
                        }
                    }

                    if (!string.IsNullOrEmpty(startupMessage))
                    {
                        Console.WriteLine(startupMessage);
                    }
                }

                await host.WaitForTokenShutdownAsync(token);
            }
            finally
            {
                if (host is IAsyncDisposable asyncDisposable)
                {
                    await asyncDisposable.DisposeAsync().ConfigureAwait(false);
                }
                else
                {
                    host.Dispose();
                }
            }
        }
예제 #9
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="IWebHost"/>.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task WaitForShutdownAsync(this IWebHost host, CancellationToken token = default)
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token))
            {
                using (var lifetime = new WebHostLifetime(cts, done, shutdownMessage: string.Empty))
                {
                    try
                    {
                        await host.WaitForTokenShutdownAsync(cts.Token);
                    }
                    finally
                    {
                        done.Set();
                    }
                }
            }
        }