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

Move Program.Main into WebApplication.Start. #444

Merged
merged 1 commit into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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<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.Run(args);
}
}
}
85 changes: 85 additions & 0 deletions src/Microsoft.AspNet.Hosting/WebApplication.cs
Original file line number Diff line number Diff line change
@@ -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<TStartup>()
{
Run(typeof(TStartup), null);
}

public static void Run<TStartup>(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<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();
}
}
}
}