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

Commit

Permalink
Clean up doc comments and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo committed Apr 19, 2017
1 parent a6b910b commit b26e49b
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -58,8 +57,8 @@ public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, strin


return hostBuilder
.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
.UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName);
.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
.UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName);
}

/// <summary>
Expand Down
26 changes: 20 additions & 6 deletions src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public interface IWebHostBuilder
/// </summary>
IWebHost Build();

// TODO: update these doc comments to indicate what may be null during configuration.

/// <summary>
/// Adds a delegate for configuring the <see cref="IConfigurationBuilder"/> that will construct an <see cref="IConfiguration"/>.
/// </summary>
/// <param name="configureDelegate">The delegate for configuring the <see cref="IConfigurationBuilder" /> that will be used to construct an <see cref="IConfiguration" />.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="IConfiguration"/> and <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> are uninitialized at this stage.
/// The <see cref="IConfigurationBuilder"/> is pre-populated with the settings of the <see cref="IWebHostBuilder"/>.
/// </remarks>
IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate);

/// <summary>
Expand All @@ -47,20 +49,29 @@ public interface IWebHostBuilder
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
/// <typeparam name="T">
/// The type of <see cref="ILoggerFactory"/> to configure.
/// The delegate will not execute if the type provided does not match the <see cref="ILoggerFactory"/> used by the <see cref="IWebHostBuilder"/>
/// </typeparam>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> configureLogging) where T : ILoggerFactory;

/// <summary>
/// Specify the delegate that is used to configure the services of the web application.
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
/// </summary>
/// <param name="configureServices">The delegate that configures the <see cref="IServiceCollection"/>.</param>
/// <param name="configureServices">A delegate for configuring the <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices);

/// <summary>
/// Specify the delegate that is used to configure the services of the web application.
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
/// </summary>
/// <param name="configureServices">The delegate that configures the <see cref="IServiceCollection"/>.</param>
/// <param name="configureServices">A delegate for configuring the <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices);

Expand Down Expand Up @@ -92,6 +103,9 @@ public interface IWebHostBuilder
/// </summary>
/// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
IWebHostBuilder UseLoggerFactory(Func<WebHostBuilderContext, ILoggerFactory> createLoggerFactory);
}
}
27 changes: 21 additions & 6 deletions src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class WebHostBuilder : IWebHostBuilder
private WebHostOptions _options;
private bool _webHostBuilt;
private Func<WebHostBuilderContext, ILoggerFactory> _createLoggerFactoryDelegate;
private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureConfigurationBuilderDelegates;
private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureAppConfigurationBuilderDelegates;

public WebHostBuilderContext Context { get; }

/// <summary>
Expand All @@ -43,7 +44,7 @@ public WebHostBuilder()
_hostingEnvironment = new HostingEnvironment();
_configureServicesDelegates = new List<Action<WebHostBuilderContext, IServiceCollection>>();
_configureLoggingDelegates = new List<Action<WebHostBuilderContext, ILoggerFactory>>();
_configureConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>();
_configureAppConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>();

_config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
Expand Down Expand Up @@ -111,6 +112,9 @@ public IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory)
/// </summary>
/// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
public IWebHostBuilder UseLoggerFactory(Func<WebHostBuilderContext, ILoggerFactory> createLoggerFactory)
{
if (createLoggerFactory == null)
Expand Down Expand Up @@ -175,7 +179,14 @@ public IWebHostBuilder ConfigureLogging(Action<ILoggerFactory> configureLogging)
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
/// <typeparam name="T">
/// The type of <see cref="ILoggerFactory"/> to configure.
/// The delegate will not execute if the type provided does not match the <see cref="ILoggerFactory"/> used by the <see cref="IWebHostBuilder"/>
/// </typeparam>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> is uninitialized at this stage.
/// </remarks>
public IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> configureLogging) where T : ILoggerFactory
{
if (configureLogging == null)
Expand All @@ -198,14 +209,18 @@ public IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> conf
/// </summary>
/// <param name="configureDelegate">The delegate for configuring the <see cref="IConfigurationBuilder" /> that will be used to construct an <see cref="IConfiguration" />.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
/// <remarks>
/// The <see cref="IConfiguration"/> and <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> are uninitialized at this stage.
/// The <see cref="IConfigurationBuilder"/> is pre-populated with the settings of the <see cref="IWebHostBuilder"/>.
/// </remarks>
public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
{
if (configureDelegate == null)
{
throw new ArgumentNullException(nameof(configureDelegate));
}

_configureConfigurationBuilderDelegates.Add(configureDelegate);
_configureAppConfigurationBuilderDelegates.Add(configureDelegate);
return this;
}

Expand Down Expand Up @@ -270,14 +285,15 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta

var services = new ServiceCollection();
services.AddSingleton(_hostingEnvironment);
services.AddSingleton(Context);

var builder = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath)
.AddInMemoryCollection(_config.AsEnumerable());

foreach (var configureConfiguration in _configureConfigurationBuilderDelegates)
foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates)
{
configureConfiguration(Context, builder);
configureAppConfiguration(Context, builder);
}

var configuration = builder.Build();
Expand Down Expand Up @@ -322,7 +338,6 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
}
}

// Kept for back-compat, will remove once ConfigureLogging is removed.
foreach (var configureLogging in _configureLoggingDelegates)
{
configureLogging(Context, loggerFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static IWebHostBuilder UseDefaultServiceProvider(this IWebHostBuilder hos
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, LoggerFactory> configureLogging)
{
return hostBuilder.ConfigureLogging<LoggerFactory>(configureLogging);
return hostBuilder.ConfigureLogging(configureLogging);
}

/// <summary>
Expand Down
Loading

0 comments on commit b26e49b

Please sign in to comment.