private static void Run(this OicHost host, CancellationToken token, string shutdownMessage) { using (host) { host.Start(); var hostingEnvironment = host.Services.GetService <IHostingEnvironment>(); var applicationLifetime = host.Services.GetService <IApplicationLifetime>(); Console.WriteLine($"Hosting environment: {hostingEnvironment.EnvironmentName}"); //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); } token.Register(state => { ((IApplicationLifetime)state).StopApplication(); }, applicationLifetime); applicationLifetime.ApplicationStopping.WaitHandle.WaitOne(); } }
/// <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(); } }
public OicHostApplication(RequestDelegate application, OicHost host, IEnumerable<IOicContextFactory> contextFactories, ILogger<OicHostApplication> logger) { _logger = logger; _application = application ?? throw new ArgumentNullException(nameof(application)); _host = host; foreach (var contextFactory in contextFactories) { var factoryType = contextFactory.GetType().GetInterfaces().FirstOrDefault(t => t.IsConstructedGenericType); if (factoryType == null) continue; _contextFactories.Add(factoryType.GenericTypeArguments.First(), contextFactory); } }
/// <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); }