diff --git a/src/Microsoft.AspNet.Hosting/Program.cs b/src/Microsoft.AspNet.Hosting/Program.cs index 4715f818..80fa43f7 100644 --- a/src/Microsoft.AspNet.Hosting/Program.cs +++ b/src/Microsoft.AspNet.Hosting/Program.cs @@ -1,65 +1,13 @@ // 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; -using Microsoft.AspNet.Server.Features; -using Microsoft.Dnx.Compilation; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.PlatformAbstractions; - namespace Microsoft.AspNet.Hosting { public class Program { - private const string HostingJsonFile = "Microsoft.AspNet.Hosting.json"; - private const string ConfigFileKey = "config"; - public static void Main(string[] args) { - // 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; - - var appBasePath = PlatformServices.Default.Application.ApplicationBasePath; - var config = new ConfigurationBuilder() - .SetBasePath(appBasePath) - .AddJsonFile(configFilePath, optional: true) - .AddEnvironmentVariables() - .AddCommandLine(args) - .Build(); - - var host = new WebHostBuilder(config, captureStartupErrors: true).Build(); - 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 appLifetime = app.Services.GetRequiredService(); - - Console.CancelKeyPress += (sender, eventArgs) => - { - appLifetime.StopApplication(); - // Don't terminate the process immediately, wait for the Main thread to exit gracefully. - eventArgs.Cancel = true; - }; - - appLifetime.ApplicationStopping.WaitHandle.WaitOne(); - } + WebApplication.Run(args); } } } diff --git a/src/Microsoft.AspNet.Hosting/WebApplication.cs b/src/Microsoft.AspNet.Hosting/WebApplication.cs new file mode 100644 index 00000000..fea0d583 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/WebApplication.cs @@ -0,0 +1,85 @@ +// 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; +using Microsoft.AspNet.Server.Features; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNet.Hosting +{ + public class WebApplication + { + private const string HostingJsonFile = "Microsoft.AspNet.Hosting.json"; + private const string ConfigFileKey = "config"; + + public static void Run(string[] args) + { + Run(startupType: null, args: args); + } + + public static void Run() + { + Run(typeof(TStartup), null); + } + + public static void Run(string[] args) + { + Run(typeof(TStartup), args); + } + + public static void Run(Type startupType) + { + Run(startupType, null); + } + + public static void Run(Type startupType, string[] args) + { + // 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; + + var config = new ConfigurationBuilder() + .AddJsonFile(configFilePath, optional: true) + .AddEnvironmentVariables() + .AddCommandLine(args) + .Build(); + + var hostBuilder = new WebHostBuilder(config, captureStartupErrors: true); + if (startupType != null) + { + hostBuilder.UseStartup(startupType); + } + var host = hostBuilder.Build(); + 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 appLifetime = app.Services.GetRequiredService(); + + Console.CancelKeyPress += (sender, eventArgs) => + { + appLifetime.StopApplication(); + // Don't terminate the process immediately, wait for the Main thread to exit gracefully. + eventArgs.Cancel = true; + }; + + appLifetime.ApplicationStopping.WaitHandle.WaitOne(); + } + } + } +}