Skip to content
This repository was archived by the owner on Nov 21, 2018. It is now read-only.

WebHost static API updates #40

Merged
merged 1 commit into from
Apr 24, 2017
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
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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" />
Expand Down
22 changes: 22 additions & 0 deletions samples/SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +21,8 @@ public static void Main(string[] args)

CustomRouter();

CustomApplicationBuilder();

StartupClass(args);
}

Expand Down Expand Up @@ -59,6 +63,24 @@ private static void CustomRouter()
}
}

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
Expand Down
1 change: 1 addition & 0 deletions samples/SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore\Microsoft.AspNetCore.csproj" />
</ItemGroup>

Expand Down
12 changes: 12 additions & 0 deletions samples/SampleApp/wwwroot/htmlpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
A static HTML file.
</body>
</html>
34 changes: 23 additions & 11 deletions src/Microsoft.AspNetCore/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public static IWebHost Start(RequestDelegate app) =>
/// <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.
Expand All @@ -54,8 +57,11 @@ public static IWebHost Start(Action<IRouteBuilder> routeBuilder) =>
/// <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.
Expand All @@ -74,9 +80,9 @@ public static IWebHost StartWith(Action<IApplicationBuilder> app) =>
/// <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();

Expand All @@ -90,9 +96,14 @@ private static IWebHost StartWith(string url, Action<IServiceCollection> configu
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();

Expand All @@ -109,7 +120,7 @@ private static IWebHost StartWith(string url, Action<IServiceCollection> configu
/// 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>
Expand All @@ -128,7 +139,7 @@ public static IWebHostBuilder CreateDefaultBuilder() =>
/// 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>
Expand Down Expand Up @@ -165,6 +176,7 @@ public static IWebHostBuilder CreateDefaultBuilder(string[] args)
.ConfigureLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.ConfigureServices(services =>
Expand Down