Expose WebHostBuilderContext in UseDefaultServiceProvider (#1043)
This commit is contained in:
parent
a7221ca766
commit
6d42b2fe86
|
|
@ -88,10 +88,21 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
|
||||
public static IWebHostBuilder UseDefaultServiceProvider(this IWebHostBuilder hostBuilder, Action<ServiceProviderOptions> configure)
|
||||
{
|
||||
return hostBuilder.ConfigureServices(services =>
|
||||
return hostBuilder.UseDefaultServiceProvider((context, options) => configure(options));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the default service provider
|
||||
/// </summary>
|
||||
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param>
|
||||
/// <param name="configure">A callback used to configure the <see cref="ServiceProviderOptions"/> for the default <see cref="IServiceProvider"/>.</param>
|
||||
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
|
||||
public static IWebHostBuilder UseDefaultServiceProvider(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, ServiceProviderOptions> configure)
|
||||
{
|
||||
return hostBuilder.ConfigureServices((context, services) =>
|
||||
{
|
||||
var options = new ServiceProviderOptions();
|
||||
configure(options);
|
||||
configure(context, options);
|
||||
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(new DefaultServiceProviderFactory(options)));
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,6 +185,34 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
Assert.Throws<InvalidOperationException>(() => hostBuilder.Build());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConfigureDefaultServiceProviderWithContext()
|
||||
{
|
||||
var configurationCallbackCalled = false;
|
||||
var hostBuilder = new WebHostBuilder()
|
||||
.UseServer(new TestServer())
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddTransient<ServiceD>();
|
||||
s.AddScoped<ServiceC>();
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.ApplicationServices.GetRequiredService<ServiceC>();
|
||||
})
|
||||
.UseDefaultServiceProvider((context, options) =>
|
||||
{
|
||||
Assert.NotNull(context.HostingEnvironment);
|
||||
Assert.NotNull(context.Configuration);
|
||||
Assert.NotNull(context.LoggerFactory);
|
||||
configurationCallbackCalled = true;
|
||||
options.ValidateScopes = true;
|
||||
});
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => hostBuilder.Build());
|
||||
Assert.True(configurationCallbackCalled);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UseLoggerFactoryHonored()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue