Added overloads without the HostingContext (#1220)

* Added overloads without the HostingContext
This commit is contained in:
David Fowler 2017-09-18 14:48:45 -07:00 committed by GitHub
parent 712c992ca8
commit 37e122a0c6
3 changed files with 80 additions and 33 deletions

View File

@ -61,6 +61,53 @@ namespace Microsoft.Extensions.Hosting
return hostBuilder.ConfigureServices((context, collection) => collection.AddLogging(builder => configureLogging(context, builder))); return hostBuilder.ConfigureServices((context, collection) => collection.AddLogging(builder => configureLogging(context, builder)));
} }
/// <summary>
/// Adds a delegate for configuring the provided <see cref="ILoggingBuilder"/>. This may be called multiple times.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder" /> to configure.</param>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggingBuilder"/>.</param>
/// <returns>The same instance of the <see cref="IHostBuilder"/> for chaining.</returns>
public static IHostBuilder ConfigureLogging(this IHostBuilder hostBuilder, Action<ILoggingBuilder> configureLogging)
{
return hostBuilder.ConfigureServices((context, collection) => collection.AddLogging(builder => configureLogging(builder)));
}
/// <summary>
/// Sets up the configuration for the remainder of the build process and application. This can be called multiple times and
/// the results will be additive. The results will be available at <see cref="HostBuilderContext.Configuration"/> for
/// subsequent operations, as well as in <see cref="IHost.Services"/>.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder" /> to configure.</param>
/// <param name="configureDelegate"></param>
/// <returns>The same instance of the <see cref="IHostBuilder"/> for chaining.</returns>
public static IHostBuilder ConfigureAppConfiguration(this IHostBuilder hostBuilder, Action<IConfigurationBuilder> configureDelegate)
{
return hostBuilder.ConfigureAppConfiguration((context, builder) => configureDelegate(builder));
}
/// <summary>
/// Adds services to the container. This can be called multiple times and the results will be additive.
/// </summary>
/// <param name="hostBuilder">The <see cref="IHostBuilder" /> to configure.</param>
/// <param name="configureDelegate"></param>
/// <returns>The same instance of the <see cref="IHostBuilder"/> for chaining.</returns>
public static IHostBuilder ConfigureServices(this IHostBuilder hostBuilder, Action<IServiceCollection> configureDelegate)
{
return hostBuilder.ConfigureServices((context, collection) => configureDelegate(collection));
}
/// <summary>
/// Enables configuring the instantiated dependency container. This can be called multiple times and
/// the results will be additive.
/// </summary>
/// <typeparam name="TContainerBuilder"></typeparam>
/// <param name="hostBuilder">The <see cref="IHostBuilder" /> to configure.</param>
/// <param name="configureDelegate"></param>
/// <returns>The same instance of the <see cref="IHostBuilder"/> for chaining.</returns>
public static IHostBuilder ConfigureContainer<TContainerBuilder>(this IHostBuilder hostBuilder, Action<TContainerBuilder> configureDelegate)
{
return hostBuilder.ConfigureContainer<TContainerBuilder>((context, builder) => configureDelegate(builder));
}
/// <summary> /// <summary>
/// Listens for Ctrl+C or SIGTERM and calls <see cref="IApplicationLifetime.StopApplication"/> to start the shutdown process. /// Listens for Ctrl+C or SIGTERM and calls <see cref="IApplicationLifetime.StopApplication"/> to start the shutdown process.
/// This will unblock extensions like RunAsync and WaitForShutdownAsync. /// This will unblock extensions like RunAsync and WaitForShutdownAsync.

View File

@ -64,7 +64,7 @@ namespace Microsoft.Extensions.Hosting
public void CanConfigureAppConfigurationAndRetrieveFromDI() public void CanConfigureAppConfigurationAndRetrieveFromDI()
{ {
var hostBuilder = new HostBuilder() var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration((_, configBuilder) => .ConfigureAppConfiguration((configBuilder) =>
{ {
configBuilder.AddInMemoryCollection( configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[] new KeyValuePair<string, string>[]
@ -72,7 +72,7 @@ namespace Microsoft.Extensions.Hosting
new KeyValuePair<string, string>("key1", "value1") new KeyValuePair<string, string>("key1", "value1")
}); });
}) })
.ConfigureAppConfiguration((_, configBuilder) => .ConfigureAppConfiguration((configBuilder) =>
{ {
configBuilder.AddInMemoryCollection( configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[] new KeyValuePair<string, string>[]
@ -80,7 +80,7 @@ namespace Microsoft.Extensions.Hosting
new KeyValuePair<string, string>("key2", "value2") new KeyValuePair<string, string>("key2", "value2")
}); });
}) })
.ConfigureAppConfiguration((_, configBuilder) => .ConfigureAppConfiguration((configBuilder) =>
{ {
configBuilder.AddInMemoryCollection( configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[] new KeyValuePair<string, string>[]
@ -300,7 +300,7 @@ namespace Microsoft.Extensions.Hosting
public void HostingContextContainsAppConfigurationDuringConfigureServices() public void HostingContextContainsAppConfigurationDuringConfigureServices()
{ {
var hostBuilder = new HostBuilder() var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration((context, configBuilder) => .ConfigureAppConfiguration((configBuilder) =>
configBuilder.AddInMemoryCollection( configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[] new KeyValuePair<string, string>[]
{ {
@ -318,7 +318,7 @@ namespace Microsoft.Extensions.Hosting
public void ConfigureDefaultServiceProvider() public void ConfigureDefaultServiceProvider()
{ {
var hostBuilder = new HostBuilder() var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, s) => .ConfigureServices((s) =>
{ {
s.AddTransient<ServiceD>(); s.AddTransient<ServiceD>();
s.AddScoped<ServiceC>(); s.AddScoped<ServiceC>();
@ -342,12 +342,12 @@ namespace Microsoft.Extensions.Hosting
s.AddScoped<ServiceC>(); s.AddScoped<ServiceC>();
}) })
.UseServiceProviderFactory(new FakeServiceProviderFactory()) .UseServiceProviderFactory(new FakeServiceProviderFactory())
.ConfigureContainer<FakeServiceCollection>((hostContext, container) => .ConfigureContainer<FakeServiceCollection>((container) =>
{ {
Assert.Null(container.State); Assert.Null(container.State);
container.State = "1"; container.State = "1";
}) })
.ConfigureContainer<FakeServiceCollection>((hostContext, container) => .ConfigureContainer<FakeServiceCollection>((container) =>
{ {
Assert.Equal("1", container.State); Assert.Equal("1", container.State);
container.State = "2"; container.State = "2";
@ -361,13 +361,13 @@ namespace Microsoft.Extensions.Hosting
public void CustomContainerTypeMismatchThrows() public void CustomContainerTypeMismatchThrows()
{ {
var hostBuilder = new HostBuilder() var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, s) => .ConfigureServices((s) =>
{ {
s.AddTransient<ServiceD>(); s.AddTransient<ServiceD>();
s.AddScoped<ServiceC>(); s.AddScoped<ServiceC>();
}) })
.UseServiceProviderFactory(new FakeServiceProviderFactory()) .UseServiceProviderFactory(new FakeServiceProviderFactory())
.ConfigureContainer<IServiceCollection>((hostContext, container) => .ConfigureContainer<IServiceCollection>((container) =>
{ {
}); });
Assert.Throws<InvalidCastException>(() => hostBuilder.Build()); Assert.Throws<InvalidCastException>(() => hostBuilder.Build());
@ -377,7 +377,7 @@ namespace Microsoft.Extensions.Hosting
public void HostingContextContainsAppConfigurationDuringConfigureLogging() public void HostingContextContainsAppConfigurationDuringConfigureLogging()
{ {
var hostBuilder = new HostBuilder() var hostBuilder = new HostBuilder()
.ConfigureAppConfiguration((context, configBuilder) => .ConfigureAppConfiguration((configBuilder) =>
configBuilder.AddInMemoryCollection( configBuilder.AddInMemoryCollection(
new KeyValuePair<string, string>[] new KeyValuePair<string, string>[]
{ {
@ -390,18 +390,18 @@ namespace Microsoft.Extensions.Hosting
using (hostBuilder.Build()) { } using (hostBuilder.Build()) { }
} }
[Fact] [Fact]
public void ConfigureServices_CanBeCalledMultipleTimes() public void ConfigureServices_CanBeCalledMultipleTimes()
{ {
var callCount = 0; // Verify ordering var callCount = 0; // Verify ordering
var hostBuilder = new HostBuilder() var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
Assert.Equal(0, callCount++); Assert.Equal(0, callCount++);
services.AddTransient<ServiceA>(); services.AddTransient<ServiceA>();
}) })
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
Assert.Equal(1, callCount++); Assert.Equal(1, callCount++);
services.AddTransient<ServiceB>(); services.AddTransient<ServiceB>();
@ -455,12 +455,12 @@ namespace Microsoft.Extensions.Hosting
}); });
hostBuilder.Properties.Add("key", "value"); hostBuilder.Properties.Add("key", "value");
Assert.Equal("value", hostBuilder.Properties["key"]); Assert.Equal("value", hostBuilder.Properties["key"]);
using (hostBuilder.Build()) { } using (hostBuilder.Build()) { }
} }
private class ServiceC private class ServiceC
{ {
public ServiceC(ServiceD serviceD) { } public ServiceC(ServiceD serviceD) { }

View File

@ -98,7 +98,7 @@ namespace Microsoft.Extensions.Hosting
Assert.Equal(0, service.StopCount); Assert.Equal(0, service.StopCount);
Assert.Equal(0, service.DisposeCount); Assert.Equal(0, service.DisposeCount);
} }
Assert.Equal(1, service.StartCount); Assert.Equal(1, service.StartCount);
Assert.Equal(0, service.StopCount); Assert.Equal(0, service.StopCount);
Assert.Equal(1, service.DisposeCount); Assert.Equal(1, service.DisposeCount);
@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Hosting
bool[] events2 = null; bool[] events2 = null;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
events1 = RegisterCallbacksThatThrow(services); events1 = RegisterCallbacksThatThrow(services);
events2 = RegisterCallbacksThatThrow(services); events2 = RegisterCallbacksThatThrow(services);
@ -178,7 +178,7 @@ namespace Microsoft.Extensions.Hosting
var startCancelled = new ManualResetEvent(false); var startCancelled = new ManualResetEvent(false);
FakeHostedService service; FakeHostedService service;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton<IHostedService>(_ => new FakeHostedService() services.AddSingleton<IHostedService>(_ => new FakeHostedService()
{ {
@ -222,7 +222,7 @@ namespace Microsoft.Extensions.Hosting
FakeHostedService service; FakeHostedService service;
FakeHostLifetime lifetime; FakeHostLifetime lifetime;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton<IHostedService>(_ => new FakeHostedService() services.AddSingleton<IHostedService>(_ => new FakeHostedService()
{ {
@ -281,7 +281,7 @@ namespace Microsoft.Extensions.Hosting
FakeHostedService service; FakeHostedService service;
FakeHostLifetime lifetime; FakeHostLifetime lifetime;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton<IHostedService>(_ => new FakeHostedService() services.AddSingleton<IHostedService>(_ => new FakeHostedService()
{ {
@ -344,7 +344,7 @@ namespace Microsoft.Extensions.Hosting
FakeHostedService service; FakeHostedService service;
FakeHostLifetime lifetime; FakeHostLifetime lifetime;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton<IHostedService, FakeHostedService>(); services.AddSingleton<IHostedService, FakeHostedService>();
services.AddSingleton<IHostLifetime>(_ => new FakeHostLifetime() services.AddSingleton<IHostLifetime>(_ => new FakeHostLifetime()
@ -362,7 +362,7 @@ namespace Microsoft.Extensions.Hosting
{ {
await host.StartAsync(); await host.StartAsync();
Assert.True(lifetimeRegistered.WaitOne(0)); Assert.True(lifetimeRegistered.WaitOne(0));
var appLifetime = host.Services.GetRequiredService<IApplicationLifetime>(); var appLifetime = host.Services.GetRequiredService<IApplicationLifetime>();
stoppingAction(stoppingState); stoppingAction(stoppingState);
@ -395,7 +395,7 @@ namespace Microsoft.Extensions.Hosting
FakeHostedService service; FakeHostedService service;
FakeHostLifetime lifetime; FakeHostLifetime lifetime;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton<IHostedService, FakeHostedService>(); services.AddSingleton<IHostedService, FakeHostedService>();
services.AddSingleton<IHostLifetime>(_ => new FakeHostLifetime() services.AddSingleton<IHostLifetime>(_ => new FakeHostLifetime()
@ -406,7 +406,7 @@ namespace Microsoft.Extensions.Hosting
.Build()) .Build())
{ {
await host.StartAsync(); await host.StartAsync();
service = (FakeHostedService)host.Services.GetRequiredService<IHostedService>(); service = (FakeHostedService)host.Services.GetRequiredService<IHostedService>();
Assert.Equal(1, service.StartCount); Assert.Equal(1, service.StartCount);
Assert.Equal(0, service.StopCount); Assert.Equal(0, service.StopCount);
@ -442,7 +442,7 @@ namespace Microsoft.Extensions.Hosting
{ {
FakeHostedService service; FakeHostedService service;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => services.AddSingleton<IHostedService, FakeHostedService>()) .ConfigureServices((services) => services.AddSingleton<IHostedService, FakeHostedService>())
.Build()) .Build())
{ {
var lifetime = host.Services.GetRequiredService<IApplicationLifetime>(); var lifetime = host.Services.GetRequiredService<IApplicationLifetime>();
@ -487,7 +487,7 @@ namespace Microsoft.Extensions.Hosting
}); });
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton(service.Object); services.AddSingleton(service.Object);
}) })
@ -568,7 +568,7 @@ namespace Microsoft.Extensions.Hosting
public async Task HostDisposesServiceProvider() public async Task HostDisposesServiceProvider()
{ {
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, s) => .ConfigureServices((s) =>
{ {
s.AddTransient<IFakeService, FakeService>(); s.AddTransient<IFakeService, FakeService>();
s.AddSingleton<IFakeSingletonService, FakeService>(); s.AddSingleton<IFakeSingletonService, FakeService>();
@ -639,7 +639,7 @@ namespace Microsoft.Extensions.Hosting
var disposingCalls = 0; var disposingCalls = 0;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
Action started = () => Action started = () =>
{ {
@ -675,7 +675,7 @@ namespace Microsoft.Extensions.Hosting
public async Task HostedServiceCanInjectApplicationLifetime() public async Task HostedServiceCanInjectApplicationLifetime()
{ {
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
services.AddSingleton<IHostedService, TestHostedService>(); services.AddSingleton<IHostedService, TestHostedService>();
}) })
@ -701,7 +701,7 @@ namespace Microsoft.Extensions.Hosting
var disposingCalls = 0; var disposingCalls = 0;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
Action started = () => Action started = () =>
{ {
@ -753,7 +753,7 @@ namespace Microsoft.Extensions.Hosting
var disposingCalls = 0; var disposingCalls = 0;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
Action started = () => Action started = () =>
{ {
@ -795,7 +795,7 @@ namespace Microsoft.Extensions.Hosting
bool[] events2 = null; bool[] events2 = null;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => .ConfigureServices((services) =>
{ {
events1 = RegisterCallbacksThatThrow(services); events1 = RegisterCallbacksThatThrow(services);
events2 = RegisterCallbacksThatThrow(services); events2 = RegisterCallbacksThatThrow(services);
@ -823,7 +823,7 @@ namespace Microsoft.Extensions.Hosting
{ {
int configureServicesCount = 0; int configureServicesCount = 0;
using (var host = CreateBuilder() using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) => configureServicesCount++) .ConfigureServices((services) => configureServicesCount++)
.Build()) .Build())
{ {
Assert.Equal(1, configureServicesCount); Assert.Equal(1, configureServicesCount);