From 10176373c822b8df148ee70f9180e922ea820ae8 Mon Sep 17 00:00:00 2001 From: Chris R Date: Mon, 14 Sep 2015 15:07:15 -0700 Subject: [PATCH] #351 Display hosting environment name and listening addresses on console. --- .../Internal/Application.cs | 37 +++++++++++++++++++ .../Internal/HostingEngine.cs | 15 ++------ .../Internal/IApplication.cs | 15 ++++++++ .../Internal/IHostingEngine.cs | 2 +- src/Microsoft.AspNet.Hosting/Program.cs | 20 ++++++++-- 5 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 src/Microsoft.AspNet.Hosting/Internal/Application.cs create mode 100644 src/Microsoft.AspNet.Hosting/Internal/IApplication.cs diff --git a/src/Microsoft.AspNet.Hosting/Internal/Application.cs b/src/Microsoft.AspNet.Hosting/Internal/Application.cs new file mode 100644 index 0000000000..ecbb916a6e --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/Internal/Application.cs @@ -0,0 +1,37 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Http.Features; + +namespace Microsoft.AspNet.Hosting.Internal +{ + public class Application : IApplication + { + private readonly IDisposable _stop; + private readonly IServiceProvider _services; + private readonly IFeatureCollection _server; + + public Application(IServiceProvider services, IFeatureCollection server, IDisposable stop) + { + _services = services; + _server = server; + _stop = stop; + } + + public IFeatureCollection ServerFeatures + { + get { return _server; } + } + + public IServiceProvider Services + { + get { return _services; } + } + + public void Dispose() + { + _stop.Dispose(); + } + } +} diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 516fab462e..d77831fd18 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -73,7 +73,7 @@ namespace Microsoft.AspNet.Hosting.Internal } } - public virtual IDisposable Start() + public virtual IApplication Start() { EnsureApplicationServices(); @@ -96,23 +96,14 @@ namespace Microsoft.AspNet.Hosting.Internal } }); - var serverAddresses = _serverInstance.Get(); - if (serverAddresses != null) - { - foreach (var address in serverAddresses.Addresses) - { - logger.LogInformation("Now listening on: " + address); - } - } - _applicationLifetime.NotifyStarted(); - return new Disposable(() => + return new Application(ApplicationServices, _serverInstance, new Disposable(() => { _applicationLifetime.NotifyStopping(); server.Dispose(); _applicationLifetime.NotifyStopped(); - }); + })); } private void EnsureApplicationServices() diff --git a/src/Microsoft.AspNet.Hosting/Internal/IApplication.cs b/src/Microsoft.AspNet.Hosting/Internal/IApplication.cs new file mode 100644 index 0000000000..99f807e10c --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/Internal/IApplication.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Http.Features; + +namespace Microsoft.AspNet.Hosting.Internal +{ + public interface IApplication : IDisposable + { + IFeatureCollection ServerFeatures { get; } + + IServiceProvider Services { get; } + } +} diff --git a/src/Microsoft.AspNet.Hosting/Internal/IHostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/IHostingEngine.cs index d0a3818867..4b8789a9ca 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/IHostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/IHostingEngine.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNet.Hosting.Internal { public interface IHostingEngine { - IDisposable Start(); + IApplication Start(); // Accessing this will block Use methods IServiceProvider ApplicationServices { get; } diff --git a/src/Microsoft.AspNet.Hosting/Program.cs b/src/Microsoft.AspNet.Hosting/Program.cs index 3a8a941cc2..4cde138e65 100644 --- a/src/Microsoft.AspNet.Hosting/Program.cs +++ b/src/Microsoft.AspNet.Hosting/Program.cs @@ -2,6 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Server.Features; using Microsoft.Dnx.Runtime; using Microsoft.Framework.Configuration; using Microsoft.Framework.DependencyInjection; @@ -22,7 +24,7 @@ namespace Microsoft.AspNet.Hosting public void Main(string[] args) { - // Allow the location of the ini file to be specified via a --config command line arg + // Allow the location of the json file to be specified via a --config command line arg var tempBuilder = new ConfigurationBuilder().AddCommandLine(args); var tempConfig = tempBuilder.Build(); var configFilePath = tempConfig[ConfigFileKey] ?? HostingJsonFile; @@ -35,11 +37,23 @@ namespace Microsoft.AspNet.Hosting var config = builder.Build(); var host = new WebHostBuilder(_serviceProvider, config).Build(); - using (host.Start()) + using (var app = host.Start()) { + var hostingEnv = app.Services.GetRequiredService(); + Console.WriteLine("Hosting environment: " + hostingEnv.EnvironmentName); + + var serverAddresses = app.ServerFeatures.Get(); + if (serverAddresses != null) + { + foreach (var address in serverAddresses.Addresses) + { + Console.WriteLine("Now listening on: " + address); + } + } + Console.WriteLine("Application started. Press Ctrl+C to shut down."); - var appShutdownService = host.ApplicationServices.GetRequiredService(); + var appShutdownService = app.Services.GetRequiredService(); Console.CancelKeyPress += (sender, eventArgs) => { appShutdownService.RequestShutdown();