Added overloads without the HostingContext (#1220)
* Added overloads without the HostingContext
This commit is contained in:
parent
712c992ca8
commit
37e122a0c6
|
|
@ -61,6 +61,53 @@ namespace Microsoft.Extensions.Hosting
|
|||
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>
|
||||
/// 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.
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
public void CanConfigureAppConfigurationAndRetrieveFromDI()
|
||||
{
|
||||
var hostBuilder = new HostBuilder()
|
||||
.ConfigureAppConfiguration((_, configBuilder) =>
|
||||
.ConfigureAppConfiguration((configBuilder) =>
|
||||
{
|
||||
configBuilder.AddInMemoryCollection(
|
||||
new KeyValuePair<string, string>[]
|
||||
|
|
@ -72,7 +72,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
new KeyValuePair<string, string>("key1", "value1")
|
||||
});
|
||||
})
|
||||
.ConfigureAppConfiguration((_, configBuilder) =>
|
||||
.ConfigureAppConfiguration((configBuilder) =>
|
||||
{
|
||||
configBuilder.AddInMemoryCollection(
|
||||
new KeyValuePair<string, string>[]
|
||||
|
|
@ -80,7 +80,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
new KeyValuePair<string, string>("key2", "value2")
|
||||
});
|
||||
})
|
||||
.ConfigureAppConfiguration((_, configBuilder) =>
|
||||
.ConfigureAppConfiguration((configBuilder) =>
|
||||
{
|
||||
configBuilder.AddInMemoryCollection(
|
||||
new KeyValuePair<string, string>[]
|
||||
|
|
@ -300,7 +300,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
public void HostingContextContainsAppConfigurationDuringConfigureServices()
|
||||
{
|
||||
var hostBuilder = new HostBuilder()
|
||||
.ConfigureAppConfiguration((context, configBuilder) =>
|
||||
.ConfigureAppConfiguration((configBuilder) =>
|
||||
configBuilder.AddInMemoryCollection(
|
||||
new KeyValuePair<string, string>[]
|
||||
{
|
||||
|
|
@ -318,7 +318,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
public void ConfigureDefaultServiceProvider()
|
||||
{
|
||||
var hostBuilder = new HostBuilder()
|
||||
.ConfigureServices((hostContext, s) =>
|
||||
.ConfigureServices((s) =>
|
||||
{
|
||||
s.AddTransient<ServiceD>();
|
||||
s.AddScoped<ServiceC>();
|
||||
|
|
@ -342,12 +342,12 @@ namespace Microsoft.Extensions.Hosting
|
|||
s.AddScoped<ServiceC>();
|
||||
})
|
||||
.UseServiceProviderFactory(new FakeServiceProviderFactory())
|
||||
.ConfigureContainer<FakeServiceCollection>((hostContext, container) =>
|
||||
.ConfigureContainer<FakeServiceCollection>((container) =>
|
||||
{
|
||||
Assert.Null(container.State);
|
||||
container.State = "1";
|
||||
})
|
||||
.ConfigureContainer<FakeServiceCollection>((hostContext, container) =>
|
||||
.ConfigureContainer<FakeServiceCollection>((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<ServiceD>();
|
||||
s.AddScoped<ServiceC>();
|
||||
})
|
||||
.UseServiceProviderFactory(new FakeServiceProviderFactory())
|
||||
.ConfigureContainer<IServiceCollection>((hostContext, container) =>
|
||||
.ConfigureContainer<IServiceCollection>((container) =>
|
||||
{
|
||||
});
|
||||
Assert.Throws<InvalidCastException>(() => 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<string, string>[]
|
||||
{
|
||||
|
|
@ -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<ServiceA>();
|
||||
})
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
.ConfigureServices((services) =>
|
||||
{
|
||||
Assert.Equal(1, callCount++);
|
||||
services.AddTransient<ServiceB>();
|
||||
|
|
@ -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) { }
|
||||
|
|
|
|||
|
|
@ -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<IHostedService>(_ => 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<IHostedService>(_ => 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<IHostedService>(_ => 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<IHostedService, FakeHostedService>();
|
||||
services.AddSingleton<IHostLifetime>(_ => new FakeHostLifetime()
|
||||
|
|
@ -362,7 +362,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
{
|
||||
await host.StartAsync();
|
||||
Assert.True(lifetimeRegistered.WaitOne(0));
|
||||
|
||||
|
||||
var appLifetime = host.Services.GetRequiredService<IApplicationLifetime>();
|
||||
|
||||
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<IHostedService, FakeHostedService>();
|
||||
services.AddSingleton<IHostLifetime>(_ => new FakeHostLifetime()
|
||||
|
|
@ -406,7 +406,7 @@ namespace Microsoft.Extensions.Hosting
|
|||
.Build())
|
||||
{
|
||||
await host.StartAsync();
|
||||
|
||||
|
||||
service = (FakeHostedService)host.Services.GetRequiredService<IHostedService>();
|
||||
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<IHostedService, FakeHostedService>())
|
||||
.ConfigureServices((services) => services.AddSingleton<IHostedService, FakeHostedService>())
|
||||
.Build())
|
||||
{
|
||||
var lifetime = host.Services.GetRequiredService<IApplicationLifetime>();
|
||||
|
|
@ -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<IFakeService, FakeService>();
|
||||
s.AddSingleton<IFakeSingletonService, FakeService>();
|
||||
|
|
@ -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<IHostedService, TestHostedService>();
|
||||
})
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue