Skip to content

Make "builder.Services" reflect the final state of the ServiceCollection #34455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ private void ConfigureWebHost(IWebHostBuilder genericWebHostBuilder)
{
services.Add(s);
}

// Add any services to the user visible service collection so that they are observable
// just in case users capture the Services property. Orchard does this to get a "blueprint"
// of the service collection. The order needs to be preserved here so we clear the original
// collection and add all of the services in order.
Services.Clear();
foreach (var s in services)
{
Services.Add(s);
}
});

genericWebHostBuilder.Configure(ConfigureApplication);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,52 @@ public void WebApplicationCreate_RegistersRouting()
Assert.NotNull(linkGenerator);
}

[Fact]
public void WebApplication_CanResolveDefaultServicesFromServiceCollection()
{
var builder = WebApplication.CreateBuilder();

// Add the service collection to the service collection
builder.Services.AddSingleton(builder.Services);

var app = builder.Build();

var env0 = app.Services.GetRequiredService<IHostEnvironment>();

var env1 = app.Services.GetRequiredService<IServiceCollection>().BuildServiceProvider().GetRequiredService<IHostEnvironment>();

Assert.Equal(env0.ApplicationName, env1.ApplicationName);
Assert.Equal(env0.EnvironmentName, env1.EnvironmentName);
Assert.Equal(env0.ContentRootPath, env1.ContentRootPath);
}

[Fact]
public void WebApplication_CanResolveDefaultServicesFromServiceCollectionInCorrectOrder()
{
var builder = WebApplication.CreateBuilder();

// Add the service collection to the service collection
builder.Services.AddSingleton(builder.Services);

// We're overriding the default IHostLifetime so that we can test the order in which it's resolved.
// This should override the default IHostLifetime.
builder.Services.AddSingleton<IHostLifetime, CustomHostLifetime>();

var app = builder.Build();

var hostLifetime0 = app.Services.GetRequiredService<IHostLifetime>();
var childServiceProvider = app.Services.GetRequiredService<IServiceCollection>().BuildServiceProvider();
var hostLifetime1 = childServiceProvider.GetRequiredService<IHostLifetime>();

var hostLifetimes0 = app.Services.GetServices<IHostLifetime>().ToArray();
var hostLifetimes1 = childServiceProvider.GetServices<IHostLifetime>().ToArray();

Assert.IsType<CustomHostLifetime>(hostLifetime0);
Assert.IsType<CustomHostLifetime>(hostLifetime1);

Assert.Equal(hostLifetimes1.Length, hostLifetimes0.Length);
}

[Fact]
public void WebApplicationCreate_RegistersEventSourceLogger()
{
Expand Down Expand Up @@ -328,6 +374,19 @@ public void WebApplicationBuilder_CanSetWebRootPaths(bool useSetter)
Assert.Equal(fullWebRootPath, app.Environment.WebRootPath);
}

private class CustomHostLifetime : IHostLifetime
{
public Task StopAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task WaitForStartAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}

private class TestEventListener : EventListener
{
private volatile bool _disposed;
Expand Down