예제 #1
0
        /// <summary>
        /// Runs a web application and block the calling thread until host shutdown.
        /// </summary>
        /// <param name="host">The <see cref="OicHost"/> to run.</param>
        public static void Run(this OicHost host)
        {
            var done = new ManualResetEventSlim(false);

            using (var cts = new CancellationTokenSource())
            {
                Action shutdown = () =>
                {
                    if (!cts.IsCancellationRequested)
                    {
                        Console.WriteLine("Application is shutting down...");
                        cts.Cancel();
                    }

                    done.Wait();
                };

#if NETSTANDARD1_5
                var assemblyLoadContext = AssemblyLoadContext.GetLoadContext(typeof(WebHostExtensions).GetTypeInfo().Assembly);
                assemblyLoadContext.Unloading += context => shutdown();
#endif
                Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    shutdown();
                    // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                    eventArgs.Cancel = true;
                };

                host.Run(cts.Token, "Application started. Press Ctrl+C to shut down.");
                done.Set();
            }
        }
예제 #2
0
 /// <summary>
 /// Runs a web application and block the calling thread until token is triggered or shutdown is triggered.
 /// </summary>
 /// <param name="host">The <see cref="OicHost"/> to run.</param>
 /// <param name="token">The token to trigger shutdown.</param>
 public static void Run(this OicHost host, CancellationToken token)
 {
     host.Run(token, shutdownMessage: null);
 }