WebHost static API updates

- Specify ApplicationName when wrapping RequestDelegate or RouteBuilder
- Add debug logger by default
This commit is contained in:
John Luo 2017-04-21 19:13:33 -07:00
parent d9661ed70d
commit 3a4a3ca291
5 changed files with 59 additions and 12 deletions

View File

@ -40,6 +40,7 @@
<MetaPackagePackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<MetaPackagePackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<MetaPackagePackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<MetaPackagePackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<MetaPackagePackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
</ItemGroup>
@ -149,7 +150,6 @@
<FullMetaPackagePackageReference Include="Microsoft.Extensions.Localization" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<FullMetaPackagePackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<FullMetaPackagePackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<FullMetaPackagePackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<FullMetaPackagePackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<FullMetaPackagePackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="$(AspNetCoreVersion)" PrivateAssets="None" />
<FullMetaPackagePackageReference Include="Microsoft.Extensions.ObjectPool" Version="$(AspNetCoreVersion)" PrivateAssets="None" />

View File

@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
@ -19,6 +21,8 @@ namespace SampleApp
CustomRouter();
CustomApplicationBuilder();
StartupClass(args);
}
@ -59,6 +63,24 @@ namespace SampleApp
}
}
private static void CustomApplicationBuilder()
{
// Using a application builder
using (WebHost.StartWith(app =>
{
app.UseStaticFiles();
app.Run(async context =>
{
await context.Response.WriteAsync("Hello, World!");
});
}))
{
//host.WaitForShutdown(); // TODO: https://github.com/aspnet/Hosting/issues/1022
Console.WriteLine("Running CustomApplicationBuilder: Press any key to shutdown and start the next sample...");
Console.ReadKey();
}
}
private static void StartupClass(string[] args)
{
// Using defaults with a Startup class

View File

@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore\Microsoft.AspNetCore.csproj" />
</ItemGroup>

View File

@ -0,0 +1,12 @@

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
A static HTML file.
</body>
</html>

View File

@ -35,8 +35,11 @@ namespace Microsoft.AspNetCore
/// <param name="url">The URL the hosted application will listen on.</param>
/// <param name="app">A delegate that handles requests to the application.</param>
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
public static IWebHost Start(string url, RequestDelegate app) =>
StartWith(url, appBuilder => appBuilder.Run(app));
public static IWebHost Start(string url, RequestDelegate app)
{
var startupAssemblyName = app.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
return StartWith(url: url, configureServices: null, app: appBuilder => appBuilder.Run(app), applicationName: startupAssemblyName);
}
/// <summary>
/// Initializes and starts a new <see cref="IWebHost"/> with pre-configured defaults.
@ -54,8 +57,11 @@ namespace Microsoft.AspNetCore
/// <param name="url">The URL the hosted application will listen on.</param>
/// <param name="routeBuilder">A delegate that configures the router for handling requests to the application.</param>
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
public static IWebHost Start(string url, Action<IRouteBuilder> routeBuilder) =>
StartWith(url, services => services.AddRouting(), app => app.UseRouter(routeBuilder));
public static IWebHost Start(string url, Action<IRouteBuilder> routeBuilder)
{
var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
return StartWith(url, services => services.AddRouting(), appBuilder => appBuilder.UseRouter(routeBuilder), applicationName: startupAssemblyName);
}
/// <summary>
/// Initializes and starts a new <see cref="IWebHost"/> with pre-configured defaults.
@ -74,9 +80,9 @@ namespace Microsoft.AspNetCore
/// <param name="app">The delegate that configures the <see cref="IApplicationBuilder"/>.</param>
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
public static IWebHost StartWith(string url, Action<IApplicationBuilder> app) =>
StartWith(url: url, configureServices: null, app: app);
StartWith(url: url, configureServices: null, app: app, applicationName: null);
private static IWebHost StartWith(string url, Action<IServiceCollection> configureServices, Action<IApplicationBuilder> app)
private static IWebHost StartWith(string url, Action<IServiceCollection> configureServices, Action<IApplicationBuilder> app, string applicationName)
{
var builder = CreateDefaultBuilder();
@ -90,9 +96,14 @@ namespace Microsoft.AspNetCore
builder.ConfigureServices(configureServices);
}
var host = builder
.Configure(app)
.Build();
builder.Configure(app);
if (!string.IsNullOrEmpty(applicationName))
{
builder.UseSetting(WebHostDefaults.ApplicationKey, applicationName);
}
var host = builder.Build();
host.Start();
@ -109,7 +120,7 @@ namespace Microsoft.AspNetCore
/// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json',
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
/// load <see cref="IConfiguration"/> from environment variables,
/// configures the <see cref="ILoggerFactory"/> to log to the console,
/// configures the <see cref="ILoggerFactory"/> to log to the console and debug output,
/// enables IIS integration,
/// and adds the developer exception page when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development'
/// </remarks>
@ -128,7 +139,7 @@ namespace Microsoft.AspNetCore
/// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
/// load <see cref="IConfiguration"/> from environment variables,
/// load <see cref="IConfiguration"/> from supplied command line args,
/// configures the <see cref="ILoggerFactory"/> to log to the console,
/// configures the <see cref="ILoggerFactory"/> to log to the console and debug output,
/// enables IIS integration,
/// and adds the developer exception page when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development'
/// </remarks>
@ -165,6 +176,7 @@ namespace Microsoft.AspNetCore
.ConfigureLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.ConfigureServices(services =>