diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs
index c98de63e..a2bca308 100644
--- a/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs
+++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs
@@ -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 @@ 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);
}
///
diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
index 013cda5c..c22c2edb 100644
--- a/src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
+++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
@@ -27,13 +27,15 @@ public interface IWebHostBuilder
///
IWebHost Build();
- // TODO: update these doc comments to indicate what may be null during configuration.
-
///
/// Adds a delegate for configuring the that will construct an .
///
/// The delegate for configuring the that will be used to construct an .
/// The .
+ ///
+ /// The and on the are uninitialized at this stage.
+ /// The is pre-populated with the settings of the .
+ ///
IWebHostBuilder ConfigureAppConfiguration(Action configureDelegate);
///
@@ -47,20 +49,29 @@ public interface IWebHostBuilder
/// Adds a delegate for configuring the provided . This may be called multiple times.
///
/// The delegate that configures the .
+ ///
+ /// The type of to configure.
+ /// The delegate will not execute if the type provided does not match the used by the
+ ///
/// The .
+ ///
+ /// The on the is uninitialized at this stage.
+ ///
IWebHostBuilder ConfigureLogging(Action configureLogging) where T : ILoggerFactory;
///
- /// 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.
///
- /// The delegate that configures the .
+ /// A delegate for configuring the .
/// The .
IWebHostBuilder ConfigureServices(Action configureServices);
///
- /// 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.
///
- /// The delegate that configures the .
+ /// A delegate for configuring the .
/// The .
IWebHostBuilder ConfigureServices(Action configureServices);
@@ -92,6 +103,9 @@ public interface IWebHostBuilder
///
/// The delegate that constructs an
/// The .
+ ///
+ /// The on the is uninitialized at this stage.
+ ///
IWebHostBuilder UseLoggerFactory(Func createLoggerFactory);
}
}
\ No newline at end of file
diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
index bcfd4284..25be4976 100644
--- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
+++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
@@ -32,7 +32,8 @@ public class WebHostBuilder : IWebHostBuilder
private WebHostOptions _options;
private bool _webHostBuilt;
private Func _createLoggerFactoryDelegate;
- private List> _configureConfigurationBuilderDelegates;
+ private List> _configureAppConfigurationBuilderDelegates;
+
public WebHostBuilderContext Context { get; }
///
@@ -43,7 +44,7 @@ public WebHostBuilder()
_hostingEnvironment = new HostingEnvironment();
_configureServicesDelegates = new List>();
_configureLoggingDelegates = new List>();
- _configureConfigurationBuilderDelegates = new List>();
+ _configureAppConfigurationBuilderDelegates = new List>();
_config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
@@ -111,6 +112,9 @@ public IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory)
///
/// The delegate that constructs an
/// The .
+ ///
+ /// The on the is uninitialized at this stage.
+ ///
public IWebHostBuilder UseLoggerFactory(Func createLoggerFactory)
{
if (createLoggerFactory == null)
@@ -175,7 +179,14 @@ public IWebHostBuilder ConfigureLogging(Action configureLogging)
/// Adds a delegate for configuring the provided . This may be called multiple times.
///
/// The delegate that configures the .
+ ///
+ /// The type of to configure.
+ /// The delegate will not execute if the type provided does not match the used by the
+ ///
/// The .
+ ///
+ /// The on the is uninitialized at this stage.
+ ///
public IWebHostBuilder ConfigureLogging(Action configureLogging) where T : ILoggerFactory
{
if (configureLogging == null)
@@ -198,6 +209,10 @@ public IWebHostBuilder ConfigureLogging(Action conf
///
/// The delegate for configuring the that will be used to construct an .
/// The .
+ ///
+ /// The and on the are uninitialized at this stage.
+ /// The is pre-populated with the settings of the .
+ ///
public IWebHostBuilder ConfigureAppConfiguration(Action configureDelegate)
{
if (configureDelegate == null)
@@ -205,7 +220,7 @@ public IWebHostBuilder ConfigureAppConfiguration(ActionThe .
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action configureLogging)
{
- return hostBuilder.ConfigureLogging(configureLogging);
+ return hostBuilder.ConfigureLogging(configureLogging);
}
///
diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
index 4889ed7c..5cc6c1e7 100644
--- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
+++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
@@ -35,10 +35,11 @@ public void Build_honors_UseStartup_with_string()
{
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 @@ public async Task StartupMissing_Fallback()
{
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 @@ public void DefaultCreatesLoggerFactory()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
-
- Assert.NotNull(host.Services.GetService());
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ Assert.NotNull(host.Services.GetService());
+ }
}
[Fact]
@@ -195,9 +196,10 @@ public void UseLoggerFactoryHonored()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
-
- Assert.Same(loggerFactory, host.Services.GetService());
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ Assert.Same(loggerFactory, host.Services.GetService());
+ }
}
[Fact]
@@ -216,8 +218,10 @@ public void MultipleConfigureLoggingInvokedInOrder()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- Assert.Equal(2, callCount);
+ using (hostBuilder.Build())
+ {
+ Assert.Equal(2, callCount);
+ }
}
[Fact]
@@ -230,9 +234,10 @@ public void UseLoggerFactoryDelegateIsHonored()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
-
- Assert.Same(loggerFactory, host.Services.GetService());
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ Assert.Same(loggerFactory, host.Services.GetService());
+ }
}
[Fact]
@@ -252,21 +257,23 @@ public void UseLoggerFactoryFuncAndConfigureLoggingCompose()
})
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- Assert.Equal(2, callCount);
- Assert.Same(loggerFactory, host.Services.GetService());
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ Assert.Equal(2, callCount);
+ Assert.Same(loggerFactory, host.Services.GetService());
+ }
}
[Fact]
- public void HostingContextCanBeUsed()
+ public void HostingContextContainsAppConfigurationDuringConfigureLogging()
{
var hostBuilder = new WebHostBuilder()
- .ConfigureAppConfiguration((context, configBuilder) => configBuilder
- .AddInMemoryCollection(
- new KeyValuePair[]
- {
- new KeyValuePair("key1", "value1")
- }))
+ .ConfigureAppConfiguration((context, configBuilder) =>
+ configBuilder.AddInMemoryCollection(
+ new KeyValuePair[]
+ {
+ new KeyValuePair("key1", "value1")
+ }))
.ConfigureLogging((context, factory) =>
{
Assert.Equal("value1", context.Configuration["key1"]);
@@ -274,10 +281,27 @@ public void HostingContextCanBeUsed()
.UseServer(new TestServer())
.UseStartup();
- hostBuilder.Build();
+ using (hostBuilder.Build()) { }
+ }
+
+ [Fact]
+ public void HostingContextContainsAppConfigurationDuringConfigureServices()
+ {
+ var hostBuilder = new WebHostBuilder()
+ .ConfigureAppConfiguration((context, configBuilder) =>
+ configBuilder.AddInMemoryCollection(
+ new KeyValuePair[]
+ {
+ new KeyValuePair("key1", "value1")
+ }))
+ .ConfigureServices((context, factory) =>
+ {
+ Assert.Equal("value1", context.Configuration["key1"]);
+ })
+ .UseServer(new TestServer())
+ .UseStartup();
- //Verify property on builder is set.
- Assert.Equal("value1", hostBuilder.Context.Configuration["key1"]);
+ using (hostBuilder.Build()) { }
}
[Fact]
@@ -293,8 +317,10 @@ public void ConfigureLoggingCalledIfLoggerFactoryTypeMatches()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- Assert.Equal(1, callCount);
+ using (hostBuilder.Build())
+ {
+ Assert.Equal(1, callCount);
+ }
}
[Fact]
@@ -310,8 +336,10 @@ public void ConfigureLoggingNotCalledIfLoggerFactoryTypeDoesNotMatches()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- Assert.Equal(0, callCount);
+ using (hostBuilder.Build())
+ {
+ Assert.Equal(0, callCount);
+ }
}
[Fact]
@@ -325,8 +353,11 @@ public void CanUseCustomLoggerFactory()
})
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- Assert.IsType(typeof(CustomLoggerFactory), host.Services.GetService());
+
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ Assert.IsType(typeof(CustomLoggerFactory), host.Services.GetService());
+ }
}
[Fact]
@@ -335,9 +366,11 @@ public void ThereIsAlwaysConfiguration()
var hostBuilder = new WebHostBuilder()
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- Assert.NotNull(host.Services.GetService());
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ Assert.NotNull(host.Services.GetService());
+ }
}
[Fact]
@@ -352,7 +385,8 @@ public void ConfigureConfigurationSettingsPropagated()
})
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
+
+ using (hostBuilder.Build()) { }
}
[Fact]
@@ -371,11 +405,13 @@ public void CanConfigureConfigurationAndRetrieveFromDI()
})
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
- var config = host.Services.GetService();
- Assert.NotNull(config);
- Assert.Equal("value1", config["key1"]);
+ using (var host = (WebHost)hostBuilder.Build())
+ {
+ var config = host.Services.GetService();
+ Assert.NotNull(config);
+ Assert.Equal("value1", config["key1"]);
+ }
}
[Fact]
@@ -419,11 +455,13 @@ public void ConfigureServices_CanBeCalledMultipleTimes()
})
.Configure(app => { });
- var host = hostBuilder.Build();
- Assert.Equal(2, callCount);
+ using (var host = hostBuilder.Build())
+ {
+ Assert.Equal(2, callCount);
- Assert.NotNull(host.Services.GetRequiredService());
- Assert.NotNull(host.Services.GetRequiredService());
+ Assert.NotNull(host.Services.GetRequiredService());
+ Assert.NotNull(host.Services.GetRequiredService());
+ }
}
[Fact]
@@ -435,9 +473,10 @@ public void CodeBasedSettingsCodeBasedOverride()
.UseServer(new TestServer())
.UseStartup();
- 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 @@ public void CodeBasedSettingsConfigBasedOverride()
.UseServer(new TestServer())
.UseStartup();
- 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 @@ public void ConfigBasedSettingsCodeBasedOverride()
.UseServer(new TestServer())
.UseStartup();
- 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 @@ public void ConfigBasedSettingsConfigBasedOverride()
.UseServer(new TestServer())
.UseStartup();
- 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 @@ public void UseEnvironmentIsNotOverriden()
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().EnvironmentName);
+ .Build())
+ {
+ Assert.Equal(expected, host.Services.GetService().EnvironmentName);
+ }
}
[Fact]
@@ -552,14 +597,12 @@ public void BuildAndDispose()
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 @@ public void UseBasePathConfiguresBasePath()
.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().ContentRootPath);
+ .Build())
+ {
+ Assert.Equal("/", host.Services.GetService().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().ContentRootPath;
- Assert.True(Path.IsPathRooted(basePath));
- Assert.EndsWith(Path.DirectorySeparatorChar + "testroot", basePath);
+ .Build())
+ {
+ var basePath = host.Services.GetRequiredService().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().ContentRootPath);
+ .Build())
+ {
+ var appBase = PlatformServices.Default.Application.ApplicationBasePath;
+ Assert.Equal(appBase, host.Services.GetService().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();
- Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
+ .Build())
+ {
+ var hostingEnv = host.Services.GetService();
+ 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()
.UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent")
- .Build();
-
- var hostingEnv = host.Services.GetService();
- Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName);
+ .Build())
+ {
+ var hostingEnv = host.Services.GetService();
+ 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();
- Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName);
+ .Build())
+ {
+ var hostingEnv = host.Services.GetService();
+ 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();
- Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
+ .Build())
+ {
+ var hostingEnv = host.Services.GetService();
+ 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();
- Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
+ .Build())
+ {
+ var hostingEnv = host.Services.GetService();
+ Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName);
+ }
}
[Fact]
@@ -679,13 +730,13 @@ public void Build_DoesNotAllowBuildingMuiltipleTimes()
{
var builder = CreateWebHostBuilder();
var server = new TestServer();
- builder.UseServer(server)
+ using (builder.UseServer(server)
.UseStartup()
- .Build();
-
- var ex = Assert.Throws(() => builder.Build());
-
- Assert.Equal("WebHostBuilder allows creation only of a single instance of WebHost", ex.Message);
+ .Build())
+ {
+ var ex = Assert.Throws(() => builder.Build());
+ Assert.Equal("WebHostBuilder allows creation only of a single instance of WebHost", ex.Message);
+ }
}
[Fact]
@@ -693,13 +744,13 @@ public void Build_PassesSameAutoCreatedILoggerFactoryEverywhere()
{
var builder = CreateWebHostBuilder();
var server = new TestServer();
- var host = builder.UseServer(server)
+ using (var host = builder.UseServer(server)
.UseStartup()
- .Build();
-
- var startup = host.Services.GetService();
-
- Assert.Equal(startup.ConfigureLoggerFactory, startup.ConstructorLoggerFactory);
+ .Build())
+ {
+ var startup = host.Services.GetService();
+ Assert.Equal(startup.ConfigureLoggerFactory, startup.ConstructorLoggerFactory);
+ }
}
[Fact]
@@ -708,15 +759,15 @@ public void Build_PassesSamePassedILoggerFactoryEverywhere()
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()
- .Build();
-
- var startup = host.Services.GetService();
-
- Assert.Equal(factory, startup.ConfigureLoggerFactory);
- Assert.Equal(factory, startup.ConstructorLoggerFactory);
+ .Build())
+ {
+ var startup = host.Services.GetService();
+ Assert.Equal(factory, startup.ConfigureLoggerFactory);
+ Assert.Equal(factory, startup.ConstructorLoggerFactory);
+ }
}
[Fact]
@@ -726,12 +777,10 @@ public void Build_PassedILoggerFactoryNotDisposed()
var builder = CreateWebHostBuilder();
var server = new TestServer();
- var host = builder.UseServer(server)
+ using (var host = builder.UseServer(server)
.UseLoggerFactory(factory)
.UseStartup()
- .Build();
-
- host.Dispose();
+ .Build()) { }
Assert.Equal(false, factory.Disposed);
}
@@ -743,13 +792,14 @@ public void Build_DoesNotOverrideILoggerFactorySetByConfigureServices()
var builder = CreateWebHostBuilder();
var server = new TestServer();
- var host = builder.UseServer(server)
+ using (var host = builder.UseServer(server)
.ConfigureServices(collection => collection.AddSingleton(factory))
.UseStartup()
- .Build();
-
- var factoryFromHost = host.Services.GetService();
- Assert.Equal(factory, factoryFromHost);
+ .Build())
+ {
+ var factoryFromHost = host.Services.GetService();
+ Assert.Equal(factory, factoryFromHost);
+ }
}
[Fact]
@@ -761,9 +811,10 @@ public void Build_RunsHostingStartupAssembliesIfSpecified()
.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 @@ public void Build_RunsHostingStartupAssembliesBeforeApplication()
})
.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 @@ public void Build_ConfigureLoggingInHostingStartupWorks()
})
.UseServer(new TestServer());
- var host = (WebHost)builder.Build();
- var sink = host.Services.GetRequiredService();
- Assert.True(sink.Writes.Any(w => w.State.ToString() == "From startup"));
+ using (var host = (WebHost)builder.Build())
+ {
+ var sink = host.Services.GetRequiredService();
+ Assert.True(sink.Writes.Any(w => w.State.ToString() == "From startup"));
+ }
}
[Fact]
@@ -814,9 +868,10 @@ public void Build_DoesNotRunHostingStartupAssembliesDoNotRunIfNotSpecified()
.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 @@ public void Build_ThrowsIfUnloadableAssemblyNameInHostingStartupAssemblies()
.Configure(app => { })
.UseServer(new TestServer());
- var ex = Assert.Throws(() => (WebHost)builder.Build());
+ var ex = Assert.Throws(() => builder.Build());
Assert.IsType(ex.InnerExceptions[0]);
Assert.IsType(ex.InnerExceptions[0].InnerException);
}
@@ -867,8 +922,7 @@ public void HostingStartupTypeCtorThrowsIfNotIHosting()
Assert.Throws(() => new HostingStartupAttribute(typeof(WebHostTests)));
}
- private static void StaticConfigureMethod(IApplicationBuilder app)
- { }
+ private static void StaticConfigureMethod(IApplicationBuilder app) { }
private IWebHostBuilder CreateWebHostBuilder()
{
@@ -898,10 +952,7 @@ private class TestServer : IServer
IFeatureCollection IServer.Features { get; }
public RequestDelegate RequestDelegate { get; private set; }
- public void Dispose()
- {
-
- }
+ public void Dispose() { }
public Task StartAsync(IHttpApplication application, CancellationToken cancellationToken)
{
@@ -923,10 +974,7 @@ public Task StartAsync(IHttpApplication application, Cancell
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 @@ public class TestLoggerProvider : ILoggerProvider
{
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 @@ public void Dispose()
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) { }
}
}
}