Esempio n. 1
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="IAppHost"/> to run.</param>
        /// <param name="token">The token to trigger shutdown.</param>
        public static async Task RunAsync(this IAppHost host, CancellationToken token = default)
        {
            // Wait for token shutdown if it can be canceled
            if (token.CanBeCanceled)
            {
                await host.RunAsync(token, shutdownMessage : null);

                return;
            }

            // If token cannot be canceled, attach Ctrl+C and SIGTERM shutdown
            var done = new ManualResetEventSlim(false);

            using (var cts = new CancellationTokenSource())
            {
                var shutdownMessage = "Application is shutting down...";
                AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: shutdownMessage);

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

                done.Set();
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Runs a web application and block the calling thread until host shutdown.
 /// </summary>
 /// <param name="host">The <see cref="IAppHost"/> to run.</param>
 public static void Run(this IAppHost host)
 {
     host.RunAsync().GetAwaiter().GetResult();
 }