From 37e122a0c6cdf77e13d8bffa7e49ff6a9894037c Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 18 Sep 2017 14:48:45 -0700 Subject: [PATCH] Added overloads without the HostingContext (#1220) * Added overloads without the HostingContext --- .../HostingHostBuilderExtensions.cs | 47 +++++++++++++++++++ .../HostBuilderTests.cs | 30 ++++++------ .../HostTests.cs | 36 +++++++------- 3 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.Extensions.Hosting/HostingHostBuilderExtensions.cs b/src/Microsoft.Extensions.Hosting/HostingHostBuilderExtensions.cs index 55125cb888..fdf9f70988 100644 --- a/src/Microsoft.Extensions.Hosting/HostingHostBuilderExtensions.cs +++ b/src/Microsoft.Extensions.Hosting/HostingHostBuilderExtensions.cs @@ -61,6 +61,53 @@ namespace Microsoft.Extensions.Hosting return hostBuilder.ConfigureServices((context, collection) => collection.AddLogging(builder => configureLogging(context, builder))); } + /// + /// Adds a delegate for configuring the provided . This may be called multiple times. + /// + /// The to configure. + /// The delegate that configures the . + /// The same instance of the for chaining. + public static IHostBuilder ConfigureLogging(this IHostBuilder hostBuilder, Action configureLogging) + { + return hostBuilder.ConfigureServices((context, collection) => collection.AddLogging(builder => configureLogging(builder))); + } + /// + /// 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 for + /// subsequent operations, as well as in . + /// + /// The to configure. + /// + /// The same instance of the for chaining. + public static IHostBuilder ConfigureAppConfiguration(this IHostBuilder hostBuilder, Action configureDelegate) + { + return hostBuilder.ConfigureAppConfiguration((context, builder) => configureDelegate(builder)); + } + + /// + /// Adds services to the container. This can be called multiple times and the results will be additive. + /// + /// The to configure. + /// + /// The same instance of the for chaining. + public static IHostBuilder ConfigureServices(this IHostBuilder hostBuilder, Action configureDelegate) + { + return hostBuilder.ConfigureServices((context, collection) => configureDelegate(collection)); + } + + /// + /// Enables configuring the instantiated dependency container. This can be called multiple times and + /// the results will be additive. + /// + /// + /// The to configure. + /// + /// The same instance of the for chaining. + public static IHostBuilder ConfigureContainer(this IHostBuilder hostBuilder, Action configureDelegate) + { + return hostBuilder.ConfigureContainer((context, builder) => configureDelegate(builder)); + } + /// /// Listens for Ctrl+C or SIGTERM and calls to start the shutdown process. /// This will unblock extensions like RunAsync and WaitForShutdownAsync. diff --git a/test/Microsoft.Extensions.Hosting.Tests/HostBuilderTests.cs b/test/Microsoft.Extensions.Hosting.Tests/HostBuilderTests.cs index 450c91ce32..6296c815f1 100644 --- a/test/Microsoft.Extensions.Hosting.Tests/HostBuilderTests.cs +++ b/test/Microsoft.Extensions.Hosting.Tests/HostBuilderTests.cs @@ -64,7 +64,7 @@ namespace Microsoft.Extensions.Hosting public void CanConfigureAppConfigurationAndRetrieveFromDI() { var hostBuilder = new HostBuilder() - .ConfigureAppConfiguration((_, configBuilder) => + .ConfigureAppConfiguration((configBuilder) => { configBuilder.AddInMemoryCollection( new KeyValuePair[] @@ -72,7 +72,7 @@ namespace Microsoft.Extensions.Hosting new KeyValuePair("key1", "value1") }); }) - .ConfigureAppConfiguration((_, configBuilder) => + .ConfigureAppConfiguration((configBuilder) => { configBuilder.AddInMemoryCollection( new KeyValuePair[] @@ -80,7 +80,7 @@ namespace Microsoft.Extensions.Hosting new KeyValuePair("key2", "value2") }); }) - .ConfigureAppConfiguration((_, configBuilder) => + .ConfigureAppConfiguration((configBuilder) => { configBuilder.AddInMemoryCollection( new KeyValuePair[] @@ -300,7 +300,7 @@ namespace Microsoft.Extensions.Hosting public void HostingContextContainsAppConfigurationDuringConfigureServices() { var hostBuilder = new HostBuilder() - .ConfigureAppConfiguration((context, configBuilder) => + .ConfigureAppConfiguration((configBuilder) => configBuilder.AddInMemoryCollection( new KeyValuePair[] { @@ -318,7 +318,7 @@ namespace Microsoft.Extensions.Hosting public void ConfigureDefaultServiceProvider() { var hostBuilder = new HostBuilder() - .ConfigureServices((hostContext, s) => + .ConfigureServices((s) => { s.AddTransient(); s.AddScoped(); @@ -342,12 +342,12 @@ namespace Microsoft.Extensions.Hosting s.AddScoped(); }) .UseServiceProviderFactory(new FakeServiceProviderFactory()) - .ConfigureContainer((hostContext, container) => + .ConfigureContainer((container) => { Assert.Null(container.State); container.State = "1"; }) - .ConfigureContainer((hostContext, container) => + .ConfigureContainer((container) => { Assert.Equal("1", container.State); container.State = "2"; @@ -361,13 +361,13 @@ namespace Microsoft.Extensions.Hosting public void CustomContainerTypeMismatchThrows() { var hostBuilder = new HostBuilder() - .ConfigureServices((hostContext, s) => + .ConfigureServices((s) => { s.AddTransient(); s.AddScoped(); }) .UseServiceProviderFactory(new FakeServiceProviderFactory()) - .ConfigureContainer((hostContext, container) => + .ConfigureContainer((container) => { }); Assert.Throws(() => hostBuilder.Build()); @@ -377,7 +377,7 @@ namespace Microsoft.Extensions.Hosting public void HostingContextContainsAppConfigurationDuringConfigureLogging() { var hostBuilder = new HostBuilder() - .ConfigureAppConfiguration((context, configBuilder) => + .ConfigureAppConfiguration((configBuilder) => configBuilder.AddInMemoryCollection( new KeyValuePair[] { @@ -390,18 +390,18 @@ namespace Microsoft.Extensions.Hosting using (hostBuilder.Build()) { } } - + [Fact] public void ConfigureServices_CanBeCalledMultipleTimes() { var callCount = 0; // Verify ordering var hostBuilder = new HostBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { Assert.Equal(0, callCount++); services.AddTransient(); }) - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { Assert.Equal(1, callCount++); services.AddTransient(); @@ -455,12 +455,12 @@ namespace Microsoft.Extensions.Hosting }); hostBuilder.Properties.Add("key", "value"); - + Assert.Equal("value", hostBuilder.Properties["key"]); using (hostBuilder.Build()) { } } - + private class ServiceC { public ServiceC(ServiceD serviceD) { } diff --git a/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs b/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs index bf6a5e0970..d4604967d2 100644 --- a/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs +++ b/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs @@ -98,7 +98,7 @@ namespace Microsoft.Extensions.Hosting Assert.Equal(0, service.StopCount); Assert.Equal(0, service.DisposeCount); } - + Assert.Equal(1, service.StartCount); Assert.Equal(0, service.StopCount); Assert.Equal(1, service.DisposeCount); @@ -154,7 +154,7 @@ namespace Microsoft.Extensions.Hosting bool[] events2 = null; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { events1 = RegisterCallbacksThatThrow(services); events2 = RegisterCallbacksThatThrow(services); @@ -178,7 +178,7 @@ namespace Microsoft.Extensions.Hosting var startCancelled = new ManualResetEvent(false); FakeHostedService service; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(_ => new FakeHostedService() { @@ -222,7 +222,7 @@ namespace Microsoft.Extensions.Hosting FakeHostedService service; FakeHostLifetime lifetime; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(_ => new FakeHostedService() { @@ -281,7 +281,7 @@ namespace Microsoft.Extensions.Hosting FakeHostedService service; FakeHostLifetime lifetime; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(_ => new FakeHostedService() { @@ -344,7 +344,7 @@ namespace Microsoft.Extensions.Hosting FakeHostedService service; FakeHostLifetime lifetime; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(); services.AddSingleton(_ => new FakeHostLifetime() @@ -362,7 +362,7 @@ namespace Microsoft.Extensions.Hosting { await host.StartAsync(); Assert.True(lifetimeRegistered.WaitOne(0)); - + var appLifetime = host.Services.GetRequiredService(); stoppingAction(stoppingState); @@ -395,7 +395,7 @@ namespace Microsoft.Extensions.Hosting FakeHostedService service; FakeHostLifetime lifetime; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(); services.AddSingleton(_ => new FakeHostLifetime() @@ -406,7 +406,7 @@ namespace Microsoft.Extensions.Hosting .Build()) { await host.StartAsync(); - + service = (FakeHostedService)host.Services.GetRequiredService(); Assert.Equal(1, service.StartCount); Assert.Equal(0, service.StopCount); @@ -442,7 +442,7 @@ namespace Microsoft.Extensions.Hosting { FakeHostedService service; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => services.AddSingleton()) + .ConfigureServices((services) => services.AddSingleton()) .Build()) { var lifetime = host.Services.GetRequiredService(); @@ -487,7 +487,7 @@ namespace Microsoft.Extensions.Hosting }); using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(service.Object); }) @@ -568,7 +568,7 @@ namespace Microsoft.Extensions.Hosting public async Task HostDisposesServiceProvider() { using (var host = CreateBuilder() - .ConfigureServices((hostContext, s) => + .ConfigureServices((s) => { s.AddTransient(); s.AddSingleton(); @@ -639,7 +639,7 @@ namespace Microsoft.Extensions.Hosting var disposingCalls = 0; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { Action started = () => { @@ -675,7 +675,7 @@ namespace Microsoft.Extensions.Hosting public async Task HostedServiceCanInjectApplicationLifetime() { using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { services.AddSingleton(); }) @@ -701,7 +701,7 @@ namespace Microsoft.Extensions.Hosting var disposingCalls = 0; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { Action started = () => { @@ -753,7 +753,7 @@ namespace Microsoft.Extensions.Hosting var disposingCalls = 0; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { Action started = () => { @@ -795,7 +795,7 @@ namespace Microsoft.Extensions.Hosting bool[] events2 = null; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => + .ConfigureServices((services) => { events1 = RegisterCallbacksThatThrow(services); events2 = RegisterCallbacksThatThrow(services); @@ -823,7 +823,7 @@ namespace Microsoft.Extensions.Hosting { int configureServicesCount = 0; using (var host = CreateBuilder() - .ConfigureServices((hostContext, services) => configureServicesCount++) + .ConfigureServices((services) => configureServicesCount++) .Build()) { Assert.Equal(1, configureServicesCount);