Updates for WebHostBuilderContext overloads

This commit is contained in:
= 2017-04-17 03:10:49 -07:00 committed by John Luo
parent 2b07e88a58
commit 853b3847ad
7 changed files with 350 additions and 322 deletions

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -58,8 +57,8 @@ namespace Microsoft.AspNetCore.Hosting
return hostBuilder return hostBuilder
.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
.UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName); .UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName);
} }
/// <summary> /// <summary>

View File

@ -13,40 +13,21 @@ namespace Microsoft.AspNetCore.Hosting
/// </summary> /// </summary>
public interface IWebHostBuilder public interface IWebHostBuilder
{ {
/// <summary>
/// The <see cref="WebHostBuilderContext"/> used during building.
/// </summary>
/// <remarks>
/// Some properties of this type will be null whilst it is being built, most noteably the <see cref="ILoggerFactory"/> will
/// be null inside the <see cref="ConfigureAppConfiguration(Action{WebHostBuilderContext, IConfigurationBuilder})"/> method.
/// </remarks>
WebHostBuilderContext Context { get; }
/// <summary> /// <summary>
/// Builds an <see cref="IWebHost"/> which hosts a web application. /// Builds an <see cref="IWebHost"/> which hosts a web application.
/// </summary> /// </summary>
IWebHost Build(); IWebHost Build();
/// <summary> /// <summary>
/// Specify the <see cref="ILoggerFactory"/> to be used by the web host. /// Adds a delegate for configuring the <see cref="IConfigurationBuilder"/> that will construct an <see cref="IConfiguration"/>.
/// </summary> /// </summary>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to be used.</param> /// <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> /// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory); /// <remarks>
/// The <see cref="IConfiguration"/> and <see cref="ILoggerFactory"/> on the <see cref="WebHostBuilderContext"/> are uninitialized at this stage.
/// <summary> /// The <see cref="IConfigurationBuilder"/> is pre-populated with the settings of the <see cref="IWebHostBuilder"/>.
/// Specify the delegate that is used to configure the services of the web application. /// </remarks>
/// </summary> IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate);
/// <param name="configureServices">The delegate that configures 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.
/// </summary>
/// <param name="configureServices">The delegate that configures the <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices);
/// <summary> /// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times. /// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
@ -59,9 +40,39 @@ namespace Microsoft.AspNetCore.Hosting
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times. /// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary> /// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param> /// <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> /// <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; IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> configureLogging) where T : ILoggerFactory;
/// <summary>
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
/// </summary>
/// <param name="configureServices">A delegate for configuring the <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices);
/// <summary>
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
/// </summary>
/// <param name="configureServices">A delegate for configuring the <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices);
/// <summary>
/// Get the setting value from the configuration.
/// </summary>
/// <param name="key">The key of the setting to look up.</param>
/// <returns>The value the setting currently contains.</returns>
string GetSetting(string key);
/// <summary> /// <summary>
/// Add or replace a setting in the configuration. /// Add or replace a setting in the configuration.
/// </summary> /// </summary>
@ -71,11 +82,11 @@ namespace Microsoft.AspNetCore.Hosting
IWebHostBuilder UseSetting(string key, string value); IWebHostBuilder UseSetting(string key, string value);
/// <summary> /// <summary>
/// Get the setting value from the configuration. /// Specify the <see cref="ILoggerFactory"/> to be used by the web host.
/// </summary> /// </summary>
/// <param name="key">The key of the setting to look up.</param> /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to be used.</param>
/// <returns>The value the setting currently contains.</returns> /// <returns>The <see cref="IWebHostBuilder"/>.</returns>
string GetSetting(string key); IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory);
/// <summary> /// <summary>
/// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered /// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered
@ -83,14 +94,9 @@ namespace Microsoft.AspNetCore.Hosting
/// </summary> /// </summary>
/// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param> /// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns> /// <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); IWebHostBuilder UseLoggerFactory(Func<WebHostBuilderContext, ILoggerFactory> createLoggerFactory);
/// <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>
IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate);
} }
} }

View File

@ -36,12 +36,6 @@
"NewMemberId": "System.Threading.Tasks.Task StopAsync(System.Threading.CancellationToken cancellationToken)", "NewMemberId": "System.Threading.Tasks.Task StopAsync(System.Threading.CancellationToken cancellationToken)",
"Kind": "Addition" "Kind": "Addition"
}, },
{
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"NewMemberId": "Microsoft.AspNetCore.Hosting.WebHostBuilderContext get_Context()",
"Kind": "Addition"
},
{ {
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder", "OldTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder", "NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",

View File

@ -36,12 +36,6 @@
"NewMemberId": "System.Threading.Tasks.Task StopAsync(System.Threading.CancellationToken cancellationToken)", "NewMemberId": "System.Threading.Tasks.Task StopAsync(System.Threading.CancellationToken cancellationToken)",
"Kind": "Addition" "Kind": "Addition"
}, },
{
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"NewMemberId": "Microsoft.AspNetCore.Hosting.WebHostBuilderContext get_Context()",
"Kind": "Addition"
},
{ {
"OldTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder", "OldTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder", "NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",

View File

@ -30,12 +30,10 @@ namespace Microsoft.AspNetCore.Hosting
private IConfiguration _config; private IConfiguration _config;
private WebHostOptions _options; private WebHostOptions _options;
private WebHostBuilderContext _context;
private bool _webHostBuilt; private bool _webHostBuilt;
private Func<WebHostBuilderContext, ILoggerFactory> _createLoggerFactoryDelegate; private Func<WebHostBuilderContext, ILoggerFactory> _createLoggerFactoryDelegate;
private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureConfigurationBuilderDelegates; private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureAppConfigurationBuilderDelegates;
private WebHostBuilderContext _hostingContext;
public WebHostBuilderContext Context { get { return _hostingContext; } }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class. /// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
@ -45,7 +43,7 @@ namespace Microsoft.AspNetCore.Hosting
_hostingEnvironment = new HostingEnvironment(); _hostingEnvironment = new HostingEnvironment();
_configureServicesDelegates = new List<Action<WebHostBuilderContext, IServiceCollection>>(); _configureServicesDelegates = new List<Action<WebHostBuilderContext, IServiceCollection>>();
_configureLoggingDelegates = new List<Action<WebHostBuilderContext, ILoggerFactory>>(); _configureLoggingDelegates = new List<Action<WebHostBuilderContext, ILoggerFactory>>();
_configureConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>(); _configureAppConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>();
_config = new ConfigurationBuilder() _config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_") .AddEnvironmentVariables(prefix: "ASPNETCORE_")
@ -63,6 +61,21 @@ namespace Microsoft.AspNetCore.Hosting
// Try adding legacy url key, never remove this. // Try adding legacy url key, never remove this.
UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS")); UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS"));
} }
_context = new WebHostBuilderContext
{
Configuration = _config
};
}
/// <summary>
/// Get the setting value from the configuration.
/// </summary>
/// <param name="key">The key of the setting to look up.</param>
/// <returns>The value the setting currently contains.</returns>
public string GetSetting(string key)
{
return _config[key];
} }
/// <summary> /// <summary>
@ -77,16 +90,6 @@ namespace Microsoft.AspNetCore.Hosting
return this; return this;
} }
/// <summary>
/// Get the setting value from the configuration.
/// </summary>
/// <param name="key">The key of the setting to look up.</param>
/// <returns>The value the setting currently contains.</returns>
public string GetSetting(string key)
{
return _config[key];
}
/// <summary> /// <summary>
/// Specify the <see cref="ILoggerFactory"/> to be used by the web host. /// Specify the <see cref="ILoggerFactory"/> to be used by the web host.
/// </summary> /// </summary>
@ -99,7 +102,26 @@ namespace Microsoft.AspNetCore.Hosting
throw new ArgumentNullException(nameof(loggerFactory)); throw new ArgumentNullException(nameof(loggerFactory));
} }
_createLoggerFactoryDelegate = _ => loggerFactory; return UseLoggerFactory(_ => loggerFactory);
}
/// <summary>
/// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered
/// as a singleton and used by the application.
/// </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)
{
throw new ArgumentNullException(nameof(createLoggerFactory));
}
_createLoggerFactoryDelegate = createLoggerFactory;
return this; return this;
} }
@ -116,8 +138,7 @@ namespace Microsoft.AspNetCore.Hosting
throw new ArgumentNullException(nameof(configureServices)); throw new ArgumentNullException(nameof(configureServices));
} }
_configureServicesDelegates.Add((_, collection) => configureServices(collection)); return ConfigureServices((_ , services) => configureServices(services));
return this;
} }
/// <summary> /// <summary>
@ -153,34 +174,25 @@ namespace Microsoft.AspNetCore.Hosting
return this; return this;
} }
/// <summary>
/// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered
/// as a singleton and used by the application.
/// </summary>
/// <param name="createLoggerFactory">The delegate that constructs an <see cref="IConfigurationBuilder" /></param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public IWebHostBuilder UseLoggerFactory(Func<WebHostBuilderContext, ILoggerFactory> createLoggerFactory)
{
if (createLoggerFactory == null)
{
throw new ArgumentNullException(nameof(createLoggerFactory));
}
_createLoggerFactoryDelegate = createLoggerFactory;
return this;
}
/// <summary> /// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times. /// Adds a delegate for configuring the provided <see cref="ILoggerFactory"/>. This may be called multiple times.
/// </summary> /// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param> /// <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> /// <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 public IWebHostBuilder ConfigureLogging<T>(Action<WebHostBuilderContext, T> configureLogging) where T : ILoggerFactory
{ {
if (configureLogging == null) if (configureLogging == null)
{ {
throw new ArgumentNullException(nameof(configureLogging)); throw new ArgumentNullException(nameof(configureLogging));
} }
_configureLoggingDelegates.Add((context, factory) => _configureLoggingDelegates.Add((context, factory) =>
{ {
if (factory is T typedFactory) if (factory is T typedFactory)
@ -196,6 +208,10 @@ namespace Microsoft.AspNetCore.Hosting
/// </summary> /// </summary>
/// <param name="configureDelegate">The delegate for configuring the <see cref="IConfigurationBuilder" /> that will be used to construct an <see cref="IConfiguration" />.</param> /// <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> /// <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) public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
{ {
if (configureDelegate == null) if (configureDelegate == null)
@ -203,7 +219,7 @@ namespace Microsoft.AspNetCore.Hosting
throw new ArgumentNullException(nameof(configureDelegate)); throw new ArgumentNullException(nameof(configureDelegate));
} }
_configureConfigurationBuilderDelegates.Add(configureDelegate); _configureAppConfigurationBuilderDelegates.Add(configureDelegate);
return this; return this;
} }
@ -262,35 +278,31 @@ namespace Microsoft.AspNetCore.Hosting
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath); var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath);
var applicationName = _options.ApplicationName ?? appEnvironment.ApplicationName; var applicationName = _options.ApplicationName ?? appEnvironment.ApplicationName;
_hostingContext = new WebHostBuilderContext
{
Configuration = _config
};
// Initialize the hosting environment // Initialize the hosting environment
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options); _hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
_hostingContext.HostingEnvironment = _hostingEnvironment; _context.HostingEnvironment = _hostingEnvironment;
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton(_hostingEnvironment); services.AddSingleton(_hostingEnvironment);
services.AddSingleton(_context);
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath) .SetBasePath(_hostingEnvironment.ContentRootPath)
.AddInMemoryCollection(_config.AsEnumerable()); .AddInMemoryCollection(_config.AsEnumerable());
foreach (var configureConfiguration in _configureConfigurationBuilderDelegates) foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates)
{ {
configureConfiguration(_hostingContext, builder); configureAppConfiguration(_context, builder);
} }
var configuration = builder.Build(); var configuration = builder.Build();
services.AddSingleton<IConfiguration>(configuration); services.AddSingleton<IConfiguration>(configuration);
_hostingContext.Configuration = configuration; _context.Configuration = configuration;
// The configured ILoggerFactory is added as a singleton here. AddLogging below will not add an additional one. // The configured ILoggerFactory is added as a singleton here. AddLogging below will not add an additional one.
var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_hostingContext) ?? new LoggerFactory(configuration.GetSection("Logging")); var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_context) ?? new LoggerFactory(configuration.GetSection("Logging"));
services.AddSingleton(loggerFactory); services.AddSingleton(loggerFactory);
_hostingContext.LoggerFactory = loggerFactory; _context.LoggerFactory = loggerFactory;
var exceptions = new List<Exception>(); var exceptions = new List<Exception>();
@ -325,10 +337,9 @@ namespace Microsoft.AspNetCore.Hosting
} }
} }
// Kept for back-compat, will remove once ConfigureLogging is removed.
foreach (var configureLogging in _configureLoggingDelegates) foreach (var configureLogging in _configureLoggingDelegates)
{ {
configureLogging(_hostingContext, loggerFactory); configureLogging(_context, loggerFactory);
} }
//This is required to add ILogger of T. //This is required to add ILogger of T.
@ -383,7 +394,7 @@ namespace Microsoft.AspNetCore.Hosting
foreach (var configureServices in _configureServicesDelegates) foreach (var configureServices in _configureServicesDelegates)
{ {
configureServices(_hostingContext, services); configureServices(_context, services);
} }
return services; return services;

View File

@ -28,14 +28,15 @@ namespace Microsoft.AspNetCore.Hosting
var startupAssemblyName = configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name; var startupAssemblyName = configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) return hostBuilder
.ConfigureServices(services => .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
{ .ConfigureServices(services =>
services.AddSingleton<IStartup>(sp => {
{ services.AddSingleton<IStartup>(sp =>
return new DelegateStartup(sp.GetRequiredService<IServiceProviderFactory<IServiceCollection>>(), configureApp); {
}); return new DelegateStartup(sp.GetRequiredService<IServiceProviderFactory<IServiceCollection>>(), configureApp);
}); });
});
} }
@ -49,22 +50,23 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name; var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name;
return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) return hostBuilder
.ConfigureServices(services => .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
{ .ConfigureServices(services =>
if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) {
{ if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
services.AddSingleton(typeof(IStartup), startupType); {
} services.AddSingleton(typeof(IStartup), startupType);
else }
{ else
services.AddSingleton(typeof(IStartup), sp => {
{ services.AddSingleton(typeof(IStartup), sp =>
var hostingEnvironment = sp.GetRequiredService<IHostingEnvironment>(); {
return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName)); var hostingEnvironment = sp.GetRequiredService<IHostingEnvironment>();
}); return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName));
} });
}); }
});
} }
/// <summary> /// <summary>
@ -102,8 +104,7 @@ namespace Microsoft.AspNetCore.Hosting
/// <returns>The <see cref="IWebHostBuilder"/>.</returns> /// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, LoggerFactory> configureLogging) public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, LoggerFactory> configureLogging)
{ {
hostBuilder.ConfigureLogging(configureLogging); return hostBuilder.ConfigureLogging(configureLogging);
return hostBuilder;
} }
/// <summary> /// <summary>
@ -114,9 +115,7 @@ namespace Microsoft.AspNetCore.Hosting
/// <returns>The <see cref="IWebHostBuilder"/>.</returns> /// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder ConfigureLogging<T>(this IWebHostBuilder hostBuilder, Action<T> configureLogging) where T : ILoggerFactory public static IWebHostBuilder ConfigureLogging<T>(this IWebHostBuilder hostBuilder, Action<T> configureLogging) where T : ILoggerFactory
{ {
hostBuilder.ConfigureLogging<T>((_, factory) => configureLogging(factory)); return hostBuilder.ConfigureLogging<T>((_, factory) => configureLogging(factory));
return hostBuilder;
} }
} }
} }

View File

@ -35,10 +35,11 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var builder = CreateWebHostBuilder().UseServer(new TestServer()); var builder = CreateWebHostBuilder().UseServer(new TestServer());
var host = (WebHost)builder.UseStartup("MyStartupAssembly").Build(); using (var host = (WebHost)builder.UseStartup("MyStartupAssembly").Build())
{
Assert.Equal("MyStartupAssembly", host.Options.ApplicationName); Assert.Equal("MyStartupAssembly", host.Options.ApplicationName);
Assert.Equal("MyStartupAssembly", host.Options.StartupAssembly); Assert.Equal("MyStartupAssembly", host.Options.StartupAssembly);
}
} }
[Fact] [Fact]
@ -46,8 +47,7 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var builder = CreateWebHostBuilder(); var builder = CreateWebHostBuilder();
var server = new TestServer(); var server = new TestServer();
var host = builder.UseServer(server).UseStartup("MissingStartupAssembly").Build(); using (var host = builder.UseServer(server).UseStartup("MissingStartupAssembly").Build())
using (host)
{ {
await host.StartAsync(); await host.StartAsync();
await AssertResponseContains(server.RequestDelegate, "MissingStartupAssembly"); await AssertResponseContains(server.RequestDelegate, "MissingStartupAssembly");
@ -158,9 +158,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.NotNull(host.Services.GetService<ILoggerFactory>()); Assert.NotNull(host.Services.GetService<ILoggerFactory>());
}
} }
[Fact] [Fact]
@ -195,9 +196,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>()); Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
} }
[Fact] [Fact]
@ -216,8 +218,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (hostBuilder.Build())
Assert.Equal(2, callCount); {
Assert.Equal(2, callCount);
}
} }
[Fact] [Fact]
@ -230,9 +234,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>()); Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
} }
[Fact] [Fact]
@ -252,21 +257,23 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
Assert.Equal(2, callCount); {
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>()); Assert.Equal(2, callCount);
Assert.Same(loggerFactory, host.Services.GetService<ILoggerFactory>());
}
} }
[Fact] [Fact]
public void HostingContextCanBeUsed() public void HostingContextContainsAppConfigurationDuringConfigureLogging()
{ {
var hostBuilder = new WebHostBuilder() var hostBuilder = new WebHostBuilder()
.ConfigureAppConfiguration((context, configBuilder) => configBuilder .ConfigureAppConfiguration((context, configBuilder) =>
.AddInMemoryCollection( configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[] new KeyValuePair<string, string>[]
{ {
new KeyValuePair<string, string>("key1", "value1") new KeyValuePair<string, string>("key1", "value1")
})) }))
.ConfigureLogging((context, factory) => .ConfigureLogging((context, factory) =>
{ {
Assert.Equal("value1", context.Configuration["key1"]); Assert.Equal("value1", context.Configuration["key1"]);
@ -274,10 +281,27 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
hostBuilder.Build(); using (hostBuilder.Build()) { }
}
//Verify property on builder is set. [Fact]
Assert.Equal("value1", hostBuilder.Context.Configuration["key1"]); public void HostingContextContainsAppConfigurationDuringConfigureServices()
{
var hostBuilder = new WebHostBuilder()
.ConfigureAppConfiguration((context, configBuilder) =>
configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("key1", "value1")
}))
.ConfigureServices((context, factory) =>
{
Assert.Equal("value1", context.Configuration["key1"]);
})
.UseServer(new TestServer())
.UseStartup<StartupNoServices>();
using (hostBuilder.Build()) { }
} }
[Fact] [Fact]
@ -293,8 +317,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (hostBuilder.Build())
Assert.Equal(1, callCount); {
Assert.Equal(1, callCount);
}
} }
[Fact] [Fact]
@ -310,8 +336,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (hostBuilder.Build())
Assert.Equal(0, callCount); {
Assert.Equal(0, callCount);
}
} }
[Fact] [Fact]
@ -325,8 +353,11 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build();
Assert.IsType(typeof(CustomLoggerFactory), host.Services.GetService<ILoggerFactory>()); using (var host = (WebHost)hostBuilder.Build())
{
Assert.IsType(typeof(CustomLoggerFactory), host.Services.GetService<ILoggerFactory>());
}
} }
[Fact] [Fact]
@ -335,9 +366,11 @@ namespace Microsoft.AspNetCore.Hosting
var hostBuilder = new WebHostBuilder() var hostBuilder = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build();
Assert.NotNull(host.Services.GetService<IConfiguration>()); using (var host = (WebHost)hostBuilder.Build())
{
Assert.NotNull(host.Services.GetService<IConfiguration>());
}
} }
[Fact] [Fact]
@ -352,7 +385,8 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build();
using (hostBuilder.Build()) { }
} }
[Fact] [Fact]
@ -371,11 +405,13 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build();
var config = host.Services.GetService<IConfiguration>(); using (var host = (WebHost)hostBuilder.Build())
Assert.NotNull(config); {
Assert.Equal("value1", config["key1"]); var config = host.Services.GetService<IConfiguration>();
Assert.NotNull(config);
Assert.Equal("value1", config["key1"]);
}
} }
[Fact] [Fact]
@ -419,11 +455,13 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.Configure(app => { }); .Configure(app => { });
var host = hostBuilder.Build(); using (var host = hostBuilder.Build())
Assert.Equal(2, callCount); {
Assert.Equal(2, callCount);
Assert.NotNull(host.Services.GetRequiredService<ServiceA>()); Assert.NotNull(host.Services.GetRequiredService<ServiceA>());
Assert.NotNull(host.Services.GetRequiredService<ServiceB>()); Assert.NotNull(host.Services.GetRequiredService<ServiceB>());
}
} }
[Fact] [Fact]
@ -435,9 +473,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.Equal("EnvB", host.Options.Environment); Assert.Equal("EnvB", host.Options.Environment);
}
} }
[Fact] [Fact]
@ -458,9 +497,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.Equal("EnvB", host.Options.Environment); Assert.Equal("EnvB", host.Options.Environment);
}
} }
[Fact] [Fact]
@ -481,9 +521,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.Equal("EnvB", host.Options.Environment); Assert.Equal("EnvB", host.Options.Environment);
}
} }
[Fact] [Fact]
@ -513,9 +554,10 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>(); .UseStartup<StartupNoServices>();
var host = (WebHost)hostBuilder.Build(); using (var host = (WebHost)hostBuilder.Build())
{
Assert.Equal("EnvB", host.Options.Environment); Assert.Equal("EnvB", host.Options.Environment);
}
} }
[Fact] [Fact]
@ -530,14 +572,17 @@ namespace Microsoft.AspNetCore.Hosting
var config = builder.Build(); var config = builder.Build();
var expected = "MY_TEST_ENVIRONMENT"; var expected = "MY_TEST_ENVIRONMENT";
var host = new WebHostBuilder()
using (var host = new WebHostBuilder()
.UseConfiguration(config) .UseConfiguration(config)
.UseEnvironment(expected) .UseEnvironment(expected)
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests") .UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build(); .Build())
{
Assert.Equal(expected, host.Services.GetService<IHostingEnvironment>().EnvironmentName); Assert.Equal(expected, host.Services.GetService<IHostingEnvironment>().EnvironmentName);
}
} }
[Fact] [Fact]
@ -552,14 +597,12 @@ namespace Microsoft.AspNetCore.Hosting
var config = builder.Build(); var config = builder.Build();
var expected = "MY_TEST_ENVIRONMENT"; var expected = "MY_TEST_ENVIRONMENT";
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseConfiguration(config) .UseConfiguration(config)
.UseEnvironment(expected) .UseEnvironment(expected)
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests") .UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build(); .Build()) { }
host.Dispose();
} }
[Fact] [Fact]
@ -573,105 +616,113 @@ namespace Microsoft.AspNetCore.Hosting
.AddInMemoryCollection(vals); .AddInMemoryCollection(vals);
var config = builder.Build(); var config = builder.Build();
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseConfiguration(config) .UseConfiguration(config)
.UseContentRoot("/") .UseContentRoot("/")
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests") .UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build(); .Build())
{
Assert.Equal("/", host.Services.GetService<IHostingEnvironment>().ContentRootPath); Assert.Equal("/", host.Services.GetService<IHostingEnvironment>().ContentRootPath);
}
} }
[Fact] [Fact]
public void RelativeContentRootIsResolved() public void RelativeContentRootIsResolved()
{ {
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseContentRoot("testroot") .UseContentRoot("testroot")
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests") .UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build(); .Build())
{
var basePath = host.Services.GetRequiredService<IHostingEnvironment>().ContentRootPath; var basePath = host.Services.GetRequiredService<IHostingEnvironment>().ContentRootPath;
Assert.True(Path.IsPathRooted(basePath)); Assert.True(Path.IsPathRooted(basePath));
Assert.EndsWith(Path.DirectorySeparatorChar + "testroot", basePath); Assert.EndsWith(Path.DirectorySeparatorChar + "testroot", basePath);
}
} }
[Fact] [Fact]
public void DefaultContentRootIsApplicationBasePath() public void DefaultContentRootIsApplicationBasePath()
{ {
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests") .UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build(); .Build())
{
var appBase = PlatformServices.Default.Application.ApplicationBasePath; var appBase = PlatformServices.Default.Application.ApplicationBasePath;
Assert.Equal(appBase, host.Services.GetService<IHostingEnvironment>().ContentRootPath); Assert.Equal(appBase, host.Services.GetService<IHostingEnvironment>().ContentRootPath);
}
} }
[Fact] [Fact]
public void DefaultApplicationNameToStartupAssemblyName() public void DefaultApplicationNameToStartupAssemblyName()
{ {
var builder = new ConfigurationBuilder(); var builder = new ConfigurationBuilder();
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup("Microsoft.AspNetCore.Hosting.Tests") .UseStartup("Microsoft.AspNetCore.Hosting.Tests")
.Build(); .Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>(); var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName); Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
}
} }
[Fact] [Fact]
public void DefaultApplicationNameToStartupType() public void DefaultApplicationNameToStartupType()
{ {
var builder = new ConfigurationBuilder(); var builder = new ConfigurationBuilder();
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.UseStartup<StartupNoServices>() .UseStartup<StartupNoServices>()
.UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent") .UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent")
.Build(); .Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>(); var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName); Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName);
}
} }
[Fact] [Fact]
public void DefaultApplicationNameAndBasePathToStartupMethods() public void DefaultApplicationNameAndBasePathToStartupMethods()
{ {
var builder = new ConfigurationBuilder(); var builder = new ConfigurationBuilder();
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.Configure(app => { }) .Configure(app => { })
.UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent") .UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent")
.Build(); .Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>(); var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName); Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName);
}
} }
[Fact] [Fact]
public void Configure_SupportsNonStaticMethodDelegate() public void Configure_SupportsNonStaticMethodDelegate()
{ {
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.Configure(app => { }) .Configure(app => { })
.Build(); .Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>(); var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName); Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
}
} }
[Fact] [Fact]
public void Configure_SupportsStaticMethodDelegate() public void Configure_SupportsStaticMethodDelegate()
{ {
var host = new WebHostBuilder() using (var host = new WebHostBuilder()
.UseServer(new TestServer()) .UseServer(new TestServer())
.Configure(StaticConfigureMethod) .Configure(StaticConfigureMethod)
.Build(); .Build())
{
var hostingEnv = host.Services.GetService<IHostingEnvironment>(); var hostingEnv = host.Services.GetService<IHostingEnvironment>();
Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName); Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
}
} }
[Fact] [Fact]
@ -679,13 +730,13 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var builder = CreateWebHostBuilder(); var builder = CreateWebHostBuilder();
var server = new TestServer(); var server = new TestServer();
builder.UseServer(server) using (builder.UseServer(server)
.UseStartup<StartupNoServices>() .UseStartup<StartupNoServices>()
.Build(); .Build())
{
var ex = Assert.Throws<InvalidOperationException>(() => builder.Build()); var ex = Assert.Throws<InvalidOperationException>(() => builder.Build());
Assert.Equal("WebHostBuilder allows creation only of a single instance of WebHost", ex.Message);
Assert.Equal("WebHostBuilder allows creation only of a single instance of WebHost", ex.Message); }
} }
[Fact] [Fact]
@ -693,13 +744,13 @@ namespace Microsoft.AspNetCore.Hosting
{ {
var builder = CreateWebHostBuilder(); var builder = CreateWebHostBuilder();
var server = new TestServer(); var server = new TestServer();
var host = builder.UseServer(server) using (var host = builder.UseServer(server)
.UseStartup<StartupWithILoggerFactory>() .UseStartup<StartupWithILoggerFactory>()
.Build(); .Build())
{
var startup = host.Services.GetService<StartupWithILoggerFactory>(); var startup = host.Services.GetService<StartupWithILoggerFactory>();
Assert.Equal(startup.ConfigureLoggerFactory, startup.ConstructorLoggerFactory);
Assert.Equal(startup.ConfigureLoggerFactory, startup.ConstructorLoggerFactory); }
} }
[Fact] [Fact]
@ -708,15 +759,15 @@ namespace Microsoft.AspNetCore.Hosting
var factory = new LoggerFactory(); var factory = new LoggerFactory();
var builder = CreateWebHostBuilder(); var builder = CreateWebHostBuilder();
var server = new TestServer(); var server = new TestServer();
var host = builder.UseServer(server) using (var host = builder.UseServer(server)
.UseLoggerFactory(factory) .UseLoggerFactory(factory)
.UseStartup<StartupWithILoggerFactory>() .UseStartup<StartupWithILoggerFactory>()
.Build(); .Build())
{
var startup = host.Services.GetService<StartupWithILoggerFactory>(); var startup = host.Services.GetService<StartupWithILoggerFactory>();
Assert.Equal(factory, startup.ConfigureLoggerFactory);
Assert.Equal(factory, startup.ConfigureLoggerFactory); Assert.Equal(factory, startup.ConstructorLoggerFactory);
Assert.Equal(factory, startup.ConstructorLoggerFactory); }
} }
[Fact] [Fact]
@ -726,12 +777,10 @@ namespace Microsoft.AspNetCore.Hosting
var builder = CreateWebHostBuilder(); var builder = CreateWebHostBuilder();
var server = new TestServer(); var server = new TestServer();
var host = builder.UseServer(server) using (var host = builder.UseServer(server)
.UseLoggerFactory(factory) .UseLoggerFactory(factory)
.UseStartup<StartupWithILoggerFactory>() .UseStartup<StartupWithILoggerFactory>()
.Build(); .Build()) { }
host.Dispose();
Assert.Equal(false, factory.Disposed); Assert.Equal(false, factory.Disposed);
} }
@ -743,13 +792,14 @@ namespace Microsoft.AspNetCore.Hosting
var builder = CreateWebHostBuilder(); var builder = CreateWebHostBuilder();
var server = new TestServer(); var server = new TestServer();
var host = builder.UseServer(server) using (var host = builder.UseServer(server)
.ConfigureServices(collection => collection.AddSingleton<ILoggerFactory>(factory)) .ConfigureServices(collection => collection.AddSingleton<ILoggerFactory>(factory))
.UseStartup<StartupWithILoggerFactory>() .UseStartup<StartupWithILoggerFactory>()
.Build(); .Build())
{
var factoryFromHost = host.Services.GetService<ILoggerFactory>(); var factoryFromHost = host.Services.GetService<ILoggerFactory>();
Assert.Equal(factory, factoryFromHost); Assert.Equal(factory, factoryFromHost);
}
} }
[Fact] [Fact]
@ -761,9 +811,10 @@ namespace Microsoft.AspNetCore.Hosting
.Configure(app => { }) .Configure(app => { })
.UseServer(new TestServer()); .UseServer(new TestServer());
var host = (WebHost)builder.Build(); using (var host = builder.Build())
{
Assert.Equal("1", builder.GetSetting("testhostingstartup")); Assert.Equal("1", builder.GetSetting("testhostingstartup"));
}
} }
[Fact] [Fact]
@ -782,10 +833,11 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.UseServer(new TestServer()); .UseServer(new TestServer());
var host = (WebHost)builder.Build(); using (builder.Build())
{
Assert.NotNull(startup.ServiceADescriptor); Assert.NotNull(startup.ServiceADescriptor);
Assert.NotNull(startup.ServiceA); Assert.NotNull(startup.ServiceA);
}
} }
[Fact] [Fact]
@ -802,9 +854,11 @@ namespace Microsoft.AspNetCore.Hosting
}) })
.UseServer(new TestServer()); .UseServer(new TestServer());
var host = (WebHost)builder.Build(); using (var host = (WebHost)builder.Build())
var sink = host.Services.GetRequiredService<ITestSink>(); {
Assert.True(sink.Writes.Any(w => w.State.ToString() == "From startup")); var sink = host.Services.GetRequiredService<ITestSink>();
Assert.True(sink.Writes.Any(w => w.State.ToString() == "From startup"));
}
} }
[Fact] [Fact]
@ -814,9 +868,10 @@ namespace Microsoft.AspNetCore.Hosting
.Configure(app => { }) .Configure(app => { })
.UseServer(new TestServer()); .UseServer(new TestServer());
var host = (WebHost)builder.Build(); using (builder.Build())
{
Assert.Null(builder.GetSetting("testhostingstartup")); Assert.Null(builder.GetSetting("testhostingstartup"));
}
} }
[Fact] [Fact]
@ -828,7 +883,7 @@ namespace Microsoft.AspNetCore.Hosting
.Configure(app => { }) .Configure(app => { })
.UseServer(new TestServer()); .UseServer(new TestServer());
var ex = Assert.Throws<AggregateException>(() => (WebHost)builder.Build()); var ex = Assert.Throws<AggregateException>(() => builder.Build());
Assert.IsType<InvalidOperationException>(ex.InnerExceptions[0]); Assert.IsType<InvalidOperationException>(ex.InnerExceptions[0]);
Assert.IsType<FileNotFoundException>(ex.InnerExceptions[0].InnerException); Assert.IsType<FileNotFoundException>(ex.InnerExceptions[0].InnerException);
} }
@ -867,8 +922,7 @@ namespace Microsoft.AspNetCore.Hosting
Assert.Throws<ArgumentException>(() => new HostingStartupAttribute(typeof(WebHostTests))); Assert.Throws<ArgumentException>(() => new HostingStartupAttribute(typeof(WebHostTests)));
} }
private static void StaticConfigureMethod(IApplicationBuilder app) private static void StaticConfigureMethod(IApplicationBuilder app) { }
{ }
private IWebHostBuilder CreateWebHostBuilder() private IWebHostBuilder CreateWebHostBuilder()
{ {
@ -898,10 +952,7 @@ namespace Microsoft.AspNetCore.Hosting
IFeatureCollection IServer.Features { get; } IFeatureCollection IServer.Features { get; }
public RequestDelegate RequestDelegate { get; private set; } public RequestDelegate RequestDelegate { get; private set; }
public void Dispose() public void Dispose() { }
{
}
public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken)
{ {
@ -923,10 +974,7 @@ namespace Microsoft.AspNetCore.Hosting
return Task.CompletedTask; return Task.CompletedTask;
} }
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
{
return Task.CompletedTask;
}
} }
internal class StartupVerifyServiceA : IStartup internal class StartupVerifyServiceA : IStartup
@ -964,39 +1012,21 @@ namespace Microsoft.AspNetCore.Hosting
{ {
public TestSink Sink { get; set; } = new TestSink(); public TestSink Sink { get; set; } = new TestSink();
public ILogger CreateLogger(string categoryName) public ILogger CreateLogger(string categoryName) => new TestLogger(categoryName, Sink, enabled: true);
{
return new TestLogger(categoryName, Sink, enabled: true);
}
public void Dispose() public void Dispose() { }
{
}
} }
private class ServiceC private class ServiceC
{ {
public ServiceC(ServiceD serviceD) public ServiceC(ServiceD serviceD) { }
{
}
} }
internal class ServiceD internal class ServiceD { }
{
} internal class ServiceA { }
internal class ServiceA internal class ServiceB { }
{
}
internal class ServiceB
{
}
private class DisposableLoggerFactory : ILoggerFactory private class DisposableLoggerFactory : ILoggerFactory
{ {
@ -1007,14 +1037,9 @@ namespace Microsoft.AspNetCore.Hosting
public bool Disposed { get; set; } public bool Disposed { get; set; }
public ILogger CreateLogger(string categoryName) public ILogger CreateLogger(string categoryName) => NullLogger.Instance;
{
return NullLogger.Instance;
}
public void AddProvider(ILoggerProvider provider) public void AddProvider(ILoggerProvider provider) { }
{
}
} }
} }
} }