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

View File

@ -13,40 +13,21 @@ namespace Microsoft.AspNetCore.Hosting
/// </summary>
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>
/// Builds an <see cref="IWebHost"/> which hosts a web application.
/// </summary>
IWebHost Build();
/// <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>
/// <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>
IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory);
/// <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<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);
/// <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>
/// 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.
/// </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>
/// 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>
/// Add or replace a setting in the configuration.
/// </summary>
@ -71,11 +82,11 @@ namespace Microsoft.AspNetCore.Hosting
IWebHostBuilder UseSetting(string key, string value);
/// <summary>
/// Get the setting value from the configuration.
/// Specify the <see cref="ILoggerFactory"/> to be used by the web host.
/// </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);
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to be used.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory);
/// <summary>
/// Adds a delegate to construct the <see cref="ILoggerFactory"/> that will be registered
@ -83,14 +94,9 @@ namespace Microsoft.AspNetCore.Hosting
/// </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);
/// <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)",
"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",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",

View File

@ -36,12 +36,6 @@
"NewMemberId": "System.Threading.Tasks.Task StopAsync(System.Threading.CancellationToken cancellationToken)",
"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",
"NewTypeId": "public interface Microsoft.AspNetCore.Hosting.IWebHostBuilder",

View File

@ -30,12 +30,10 @@ namespace Microsoft.AspNetCore.Hosting
private IConfiguration _config;
private WebHostOptions _options;
private WebHostBuilderContext _context;
private bool _webHostBuilt;
private Func<WebHostBuilderContext, ILoggerFactory> _createLoggerFactoryDelegate;
private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureConfigurationBuilderDelegates;
private WebHostBuilderContext _hostingContext;
public WebHostBuilderContext Context { get { return _hostingContext; } }
private List<Action<WebHostBuilderContext, IConfigurationBuilder>> _configureAppConfigurationBuilderDelegates;
/// <summary>
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
@ -45,7 +43,7 @@ namespace Microsoft.AspNetCore.Hosting
_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_")
@ -63,6 +61,21 @@ namespace Microsoft.AspNetCore.Hosting
// Try adding legacy url key, never remove this.
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>
@ -77,16 +90,6 @@ namespace Microsoft.AspNetCore.Hosting
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>
/// Specify the <see cref="ILoggerFactory"/> to be used by the web host.
/// </summary>
@ -99,7 +102,26 @@ namespace Microsoft.AspNetCore.Hosting
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;
}
@ -116,8 +138,7 @@ namespace Microsoft.AspNetCore.Hosting
throw new ArgumentNullException(nameof(configureServices));
}
_configureServicesDelegates.Add((_, collection) => configureServices(collection));
return this;
return ConfigureServices((_ , services) => configureServices(services));
}
/// <summary>
@ -153,34 +174,25 @@ namespace Microsoft.AspNetCore.Hosting
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>
/// 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)
{
throw new ArgumentNullException(nameof(configureLogging));
}
_configureLoggingDelegates.Add((context, factory) =>
{
if (factory is T typedFactory)
@ -196,6 +208,10 @@ namespace Microsoft.AspNetCore.Hosting
/// </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)
@ -203,7 +219,7 @@ namespace Microsoft.AspNetCore.Hosting
throw new ArgumentNullException(nameof(configureDelegate));
}
_configureConfigurationBuilderDelegates.Add(configureDelegate);
_configureAppConfigurationBuilderDelegates.Add(configureDelegate);
return this;
}
@ -262,35 +278,31 @@ namespace Microsoft.AspNetCore.Hosting
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath);
var applicationName = _options.ApplicationName ?? appEnvironment.ApplicationName;
_hostingContext = new WebHostBuilderContext
{
Configuration = _config
};
// Initialize the hosting environment
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
_hostingContext.HostingEnvironment = _hostingEnvironment;
_context.HostingEnvironment = _hostingEnvironment;
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(_hostingContext, builder);
configureAppConfiguration(_context, builder);
}
var configuration = builder.Build();
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.
var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_hostingContext) ?? new LoggerFactory(configuration.GetSection("Logging"));
var loggerFactory = _createLoggerFactoryDelegate?.Invoke(_context) ?? new LoggerFactory(configuration.GetSection("Logging"));
services.AddSingleton(loggerFactory);
_hostingContext.LoggerFactory = loggerFactory;
_context.LoggerFactory = loggerFactory;
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)
{
configureLogging(_hostingContext, loggerFactory);
configureLogging(_context, loggerFactory);
}
//This is required to add ILogger of T.
@ -383,7 +394,7 @@ namespace Microsoft.AspNetCore.Hosting
foreach (var configureServices in _configureServicesDelegates)
{
configureServices(_hostingContext, services);
configureServices(_context, services);
}
return services;

View File

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

View File

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