Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
#430 Move Program.Main into WebApplication.Start.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Oct 22, 2015
1 parent 0394987 commit e1d3a35
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 53 deletions.
54 changes: 1 addition & 53 deletions src/Microsoft.AspNet.Hosting/Program.cs
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);
}
}
}
88 changes: 88 additions & 0 deletions src/Microsoft.AspNet.Hosting/WebApplication.cs
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();
}
}
}
}

0 comments on commit e1d3a35

Please sign in to comment.