diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs
index c98de63e97..a2bca308d4 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 @@ namespace Microsoft.AspNetCore.Hosting
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 7a9a183721..ae5dd39745 100644
--- a/src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
+++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/IWebHostBuilder.cs
@@ -13,40 +13,21 @@ namespace Microsoft.AspNetCore.Hosting
///
public interface IWebHostBuilder
{
- ///
- /// The used during building.
- ///
- ///
- /// Some properties of this type will be null whilst it is being built, most noteably the will
- /// be null inside the method.
- ///
- WebHostBuilderContext Context { get; }
-
///
/// Builds an which hosts a web application.
///
IWebHost Build();
///
- /// Specify the to be used by the web host.
+ /// Adds a delegate for configuring the that will construct an .
///
- /// The to be used.
+ /// The delegate for configuring the that will be used to construct an .
/// The .
- IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory);
-
- ///
- /// Specify the delegate that is used to configure the services of the web application.
- ///
- /// The delegate that configures the .
- /// The .
- IWebHostBuilder ConfigureServices(Action configureServices);
-
- ///
- /// Specify the delegate that is used to configure the services of the web application.
- ///
- /// The delegate that configures the .
- /// The .
- IWebHostBuilder ConfigureServices(Action configureServices);
+ ///
+ /// The and on the are uninitialized at this stage.
+ /// The is pre-populated with the settings of the .
+ ///
+ IWebHostBuilder ConfigureAppConfiguration(Action configureDelegate);
///
/// Adds a delegate for configuring the provided . This may be called multiple times.
@@ -59,9 +40,39 @@ namespace Microsoft.AspNetCore.Hosting
/// 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;
+ ///
+ /// Adds a delegate for configuring additional services for the host or web application. This may be called
+ /// multiple times.
+ ///
+ /// A delegate for configuring the .
+ /// The .
+ IWebHostBuilder ConfigureServices(Action configureServices);
+
+ ///
+ /// Adds a delegate for configuring additional services for the host or web application. This may be called
+ /// multiple times.
+ ///
+ /// A delegate for configuring the .
+ /// The .
+ IWebHostBuilder ConfigureServices(Action configureServices);
+
+ ///
+ /// Get the setting value from the configuration.
+ ///
+ /// The key of the setting to look up.
+ /// The value the setting currently contains.
+ string GetSetting(string key);
+
///
/// Add or replace a setting in the configuration.
///
@@ -71,11 +82,11 @@ namespace Microsoft.AspNetCore.Hosting
IWebHostBuilder UseSetting(string key, string value);
///
- /// Get the setting value from the configuration.
+ /// Specify the to be used by the web host.
///
- /// The key of the setting to look up.
- /// The value the setting currently contains.
- string GetSetting(string key);
+ /// The to be used.
+ /// The .
+ IWebHostBuilder UseLoggerFactory(ILoggerFactory loggerFactory);
///
/// Adds a delegate to construct the that will be registered
@@ -83,14 +94,9 @@ namespace Microsoft.AspNetCore.Hosting
///
/// The delegate that constructs an
/// The .
+ ///
+ /// The on the is uninitialized at this stage.
+ ///
IWebHostBuilder UseLoggerFactory(Func createLoggerFactory);
-
-
- ///
- /// Adds a delegate for configuring the that will construct an .
- ///
- /// The delegate for configuring the that will be used to construct an .
- /// The .
- IWebHostBuilder ConfigureAppConfiguration(Action configureDelegate);
}
}
\ No newline at end of file
diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.net45.json b/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.net45.json
index e10a179c25..e73947e54c 100644
--- a/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.net45.json
+++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.net45.json
@@ -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",
diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.netcore.json b/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.netcore.json
index 553e277848..b7204062e6 100644
--- a/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.netcore.json
+++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/exceptions.netcore.json
@@ -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",
diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
index b04522fa07..dc1bee834d 100644
--- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
+++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
@@ -30,12 +30,10 @@ namespace Microsoft.AspNetCore.Hosting
private IConfiguration _config;
private WebHostOptions _options;
+ private WebHostBuilderContext _context;
private bool _webHostBuilt;
private Func _createLoggerFactoryDelegate;
- private List> _configureConfigurationBuilderDelegates;
- private WebHostBuilderContext _hostingContext;
-
- public WebHostBuilderContext Context { get { return _hostingContext; } }
+ private List> _configureAppConfigurationBuilderDelegates;
///
/// Initializes a new instance of the class.
@@ -45,7 +43,7 @@ namespace Microsoft.AspNetCore.Hosting
_hostingEnvironment = new HostingEnvironment();
_configureServicesDelegates = new List>();
_configureLoggingDelegates = new List>();
- _configureConfigurationBuilderDelegates = new List>();
+ _configureAppConfigurationBuilderDelegates = new List>();
_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
+ };
+ }
+
+ ///
+ /// Get the setting value from the configuration.
+ ///
+ /// The key of the setting to look up.
+ /// The value the setting currently contains.
+ public string GetSetting(string key)
+ {
+ return _config[key];
}
///
@@ -77,16 +90,6 @@ namespace Microsoft.AspNetCore.Hosting
return this;
}
- ///
- /// Get the setting value from the configuration.
- ///
- /// The key of the setting to look up.
- /// The value the setting currently contains.
- public string GetSetting(string key)
- {
- return _config[key];
- }
-
///
/// Specify the to be used by the web host.
///
@@ -99,7 +102,26 @@ namespace Microsoft.AspNetCore.Hosting
throw new ArgumentNullException(nameof(loggerFactory));
}
- _createLoggerFactoryDelegate = _ => loggerFactory;
+ return UseLoggerFactory(_ => loggerFactory);
+ }
+
+ ///
+ /// Adds a delegate to construct the that will be registered
+ /// as a singleton and used by the application.
+ ///
+ /// The delegate that constructs an
+ /// The .
+ ///
+ /// The on the is uninitialized at this stage.
+ ///
+ public IWebHostBuilder UseLoggerFactory(Func 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));
}
///
@@ -153,34 +174,25 @@ namespace Microsoft.AspNetCore.Hosting
return this;
}
- ///
- /// Adds a delegate to construct the that will be registered
- /// as a singleton and used by the application.
- ///
- /// The delegate that constructs an
- /// The .
- public IWebHostBuilder UseLoggerFactory(Func createLoggerFactory)
- {
- if (createLoggerFactory == null)
- {
- throw new ArgumentNullException(nameof(createLoggerFactory));
- }
-
- _createLoggerFactoryDelegate = createLoggerFactory;
- return this;
- }
-
///
/// 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)
{
throw new ArgumentNullException(nameof(configureLogging));
}
+
_configureLoggingDelegates.Add((context, factory) =>
{
if (factory is T typedFactory)
@@ -196,6 +208,10 @@ namespace Microsoft.AspNetCore.Hosting
///
/// 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)
@@ -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(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();
@@ -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;
diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs
index d7cdd3f4cc..fecc5cc1a7 100644
--- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs
+++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs
@@ -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(sp =>
- {
- return new DelegateStartup(sp.GetRequiredService>(), configureApp);
- });
- });
+ return hostBuilder
+ .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
+ .ConfigureServices(services =>
+ {
+ services.AddSingleton(sp =>
+ {
+ return new DelegateStartup(sp.GetRequiredService>(), 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();
- 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();
+ return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName));
+ });
+ }
+ });
}
///
@@ -102,8 +104,7 @@ namespace Microsoft.AspNetCore.Hosting
/// The .
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action configureLogging)
{
- hostBuilder.ConfigureLogging(configureLogging);
- return hostBuilder;
+ return hostBuilder.ConfigureLogging(configureLogging);
}
///
@@ -114,9 +115,7 @@ namespace Microsoft.AspNetCore.Hosting
/// The .
public static IWebHostBuilder ConfigureLogging(this IWebHostBuilder hostBuilder, Action configureLogging) where T : ILoggerFactory
{
- hostBuilder.ConfigureLogging((_, factory) => configureLogging(factory));
- return hostBuilder;
+ return hostBuilder.ConfigureLogging((_, factory) => configureLogging(factory));
}
-
}
}
\ No newline at end of file
diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
index 4889ed7ceb..5cc6c1e769 100644
--- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
+++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
@@ -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();
- 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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
})
.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 @@ namespace Microsoft.AspNetCore.Hosting
.UseServer(new TestServer())
.UseStartup();
- 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[]
+ {
+ new KeyValuePair("key1", "value1")
+ }))
+ .ConfigureServices((context, factory) =>
+ {
+ Assert.Equal("value1", context.Configuration["key1"]);
+ })
+ .UseServer(new TestServer())
+ .UseStartup();
+
+ using (hostBuilder.Build()) { }
}
[Fact]
@@ -293,8 +317,10 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
})
.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 @@ namespace Microsoft.AspNetCore.Hosting
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 @@ namespace Microsoft.AspNetCore.Hosting
})
.UseServer(new TestServer())
.UseStartup();
- var host = (WebHost)hostBuilder.Build();
+
+ using (hostBuilder.Build()) { }
}
[Fact]
@@ -371,11 +405,13 @@ namespace Microsoft.AspNetCore.Hosting
})
.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 @@ 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());
- Assert.NotNull(host.Services.GetRequiredService());
+ Assert.NotNull(host.Services.GetRequiredService());
+ Assert.NotNull(host.Services.GetRequiredService());
+ }
}
[Fact]
@@ -435,9 +473,10 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ namespace Microsoft.AspNetCore.Hosting
.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 @@ 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().EnvironmentName);
+ .Build())
+ {
+ Assert.Equal(expected, host.Services.GetService().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().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 @@ namespace Microsoft.AspNetCore.Hosting
{
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 @@ namespace Microsoft.AspNetCore.Hosting
{
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 @@ 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()
- .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 @@ 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()
- .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(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 @@ 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();
- 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 @@ 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(() => (WebHost)builder.Build());
+ var ex = Assert.Throws(() => builder.Build());
Assert.IsType(ex.InnerExceptions[0]);
Assert.IsType(ex.InnerExceptions[0].InnerException);
}
@@ -867,8 +922,7 @@ namespace Microsoft.AspNetCore.Hosting
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 @@ namespace Microsoft.AspNetCore.Hosting
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 @@ 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) { }
}
}
}