This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#430 Move Program.Main into WebApplication.Start.
- Loading branch information
Showing
2 changed files
with
89 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.Dnx.Runtime; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
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<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."); | ||
|
||
var appLifetime = app.Services.GetRequiredService<IApplicationLifetime>(); | ||
|
||
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.Start(args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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.Runtime; | ||
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 Start(string[] args) | ||
{ | ||
Start(startupType: null, args: args); | ||
} | ||
|
||
public static void Start<TStartup>() | ||
{ | ||
Start(typeof(TStartup), null); | ||
} | ||
|
||
public static void Start<TStartup>(string[] args) | ||
{ | ||
Start(typeof(TStartup), args); | ||
} | ||
|
||
public static void Start(Type startupType) | ||
{ | ||
Start(startupType, null); | ||
} | ||
|
||
public static void Start(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 appBasePath = PlatformServices.Default.Application.ApplicationBasePath; | ||
var config = new ConfigurationBuilder() | ||
.SetBasePath(appBasePath) | ||
.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<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."); | ||
|
||
var appLifetime = app.Services.GetRequiredService<IApplicationLifetime>(); | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
} |