#351 Display hosting environment name and listening addresses on console.
This commit is contained in:
parent
bda0386a93
commit
10176373c8
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -73,7 +73,7 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual IDisposable Start()
|
public virtual IApplication Start()
|
||||||
{
|
{
|
||||||
EnsureApplicationServices();
|
EnsureApplicationServices();
|
||||||
|
|
||||||
|
|
@ -96,23 +96,14 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var serverAddresses = _serverInstance.Get<IServerAddressesFeature>();
|
|
||||||
if (serverAddresses != null)
|
|
||||||
{
|
|
||||||
foreach (var address in serverAddresses.Addresses)
|
|
||||||
{
|
|
||||||
logger.LogInformation("Now listening on: " + address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_applicationLifetime.NotifyStarted();
|
_applicationLifetime.NotifyStarted();
|
||||||
|
|
||||||
return new Disposable(() =>
|
return new Application(ApplicationServices, _serverInstance, new Disposable(() =>
|
||||||
{
|
{
|
||||||
_applicationLifetime.NotifyStopping();
|
_applicationLifetime.NotifyStopping();
|
||||||
server.Dispose();
|
server.Dispose();
|
||||||
_applicationLifetime.NotifyStopped();
|
_applicationLifetime.NotifyStopped();
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EnsureApplicationServices()
|
private void EnsureApplicationServices()
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,7 +7,7 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
{
|
{
|
||||||
public interface IHostingEngine
|
public interface IHostingEngine
|
||||||
{
|
{
|
||||||
IDisposable Start();
|
IApplication Start();
|
||||||
|
|
||||||
// Accessing this will block Use methods
|
// Accessing this will block Use methods
|
||||||
IServiceProvider ApplicationServices { get; }
|
IServiceProvider ApplicationServices { get; }
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.AspNet.Http.Features;
|
||||||
|
using Microsoft.AspNet.Server.Features;
|
||||||
using Microsoft.Dnx.Runtime;
|
using Microsoft.Dnx.Runtime;
|
||||||
using Microsoft.Framework.Configuration;
|
using Microsoft.Framework.Configuration;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
|
@ -22,7 +24,7 @@ namespace Microsoft.AspNet.Hosting
|
||||||
|
|
||||||
public void Main(string[] args)
|
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 tempBuilder = new ConfigurationBuilder().AddCommandLine(args);
|
||||||
var tempConfig = tempBuilder.Build();
|
var tempConfig = tempBuilder.Build();
|
||||||
var configFilePath = tempConfig[ConfigFileKey] ?? HostingJsonFile;
|
var configFilePath = tempConfig[ConfigFileKey] ?? HostingJsonFile;
|
||||||
|
|
@ -35,11 +37,23 @@ namespace Microsoft.AspNet.Hosting
|
||||||
var config = builder.Build();
|
var config = builder.Build();
|
||||||
|
|
||||||
var host = new WebHostBuilder(_serviceProvider, config).Build();
|
var host = new WebHostBuilder(_serviceProvider, config).Build();
|
||||||
using (host.Start())
|
using (var app = host.Start())
|
||||||
{
|
{
|
||||||
|
var hostingEnv = app.Services.GetRequiredService<IHostingEnvironment>();
|
||||||
|
Console.WriteLine("Hosting environment: " + hostingEnv.EnvironmentName);
|
||||||
|
|
||||||
|
var serverAddresses = app.ServerFeatures.Get<IServerAddressesFeature>();
|
||||||
|
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.");
|
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
|
||||||
|
|
||||||
var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
|
var appShutdownService = app.Services.GetRequiredService<IApplicationShutdown>();
|
||||||
Console.CancelKeyPress += (sender, eventArgs) =>
|
Console.CancelKeyPress += (sender, eventArgs) =>
|
||||||
{
|
{
|
||||||
appShutdownService.RequestShutdown();
|
appShutdownService.RequestShutdown();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue