-
Notifications
You must be signed in to change notification settings - Fork 5k
Add IHostApplicationBuilder interface #86974
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/IConfigurationManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.Configuration; | ||
|
||
/// <summary> | ||
/// Represents a mutable configuration object. | ||
/// </summary> | ||
/// <remarks> | ||
/// It is both an <see cref="IConfigurationBuilder"/> and an <see cref="IConfiguration"/>. | ||
/// As sources are added, it updates its current view of configuration. | ||
/// </remarks> | ||
public interface IConfigurationManager : IConfiguration, IConfigurationBuilder | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostApplicationBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// Represents a hosted applications and services builder which helps manage configuration, logging, lifetime, and more. | ||
/// </summary> | ||
public interface IHostApplicationBuilder | ||
{ | ||
/// <summary> | ||
/// Gets a central location for sharing state between components during the host building process. | ||
/// </summary> | ||
IDictionary<object, object> Properties { get; } | ||
|
||
/// <summary> | ||
/// Gets the set of key/value configuration properties. | ||
/// </summary> | ||
/// <remarks> | ||
/// This can be mutated by adding more configuration sources, which will update its current view. | ||
/// </remarks> | ||
IConfigurationManager Configuration { get; } | ||
|
||
/// <summary> | ||
/// Gets the information about the hosting environment an application is running in. | ||
/// </summary> | ||
IHostEnvironment Environment { get; } | ||
|
||
/// <summary> | ||
/// Gets a collection of logging providers for the application to compose. This is useful for adding new logging providers. | ||
/// </summary> | ||
ILoggingBuilder Logging { get; } | ||
|
||
/// <summary> | ||
/// Gets a collection of services for the application to compose. This is useful for adding user provided or framework provided services. | ||
/// </summary> | ||
IServiceCollection Services { get; } | ||
|
||
/// <summary> | ||
/// Registers a <see cref="IServiceProviderFactory{TContainerBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />. | ||
/// </summary> | ||
/// <param name="factory">The factory object that can create the <typeparamref name="TContainerBuilder"/> and <see cref="IServiceProvider"/>.</param> | ||
/// <param name="configure"> | ||
/// A delegate used to configure the <typeparamref T="TContainerBuilder" />. This can be used to configure services using | ||
/// APIS specific to the <see cref="IServiceProviderFactory{TContainerBuilder}" /> implementation. | ||
/// </param> | ||
/// <typeparam name="TContainerBuilder">The type of builder provided by the <see cref="IServiceProviderFactory{TContainerBuilder}" />.</typeparam> | ||
/// <remarks> | ||
/// <para> | ||
/// The <see cref="IServiceProvider"/> is created when this builder is built and so the delegate provided | ||
/// by <paramref name="configure"/> will run after all other services have been registered. | ||
/// </para> | ||
/// <para> | ||
/// Multiple calls to <see cref="ConfigureContainer{TContainerBuilder}(IServiceProviderFactory{TContainerBuilder}, Action{TContainerBuilder})"/> will replace | ||
/// the previously stored <paramref name="factory"/> and <paramref name="configure"/> delegate. | ||
/// </para> | ||
/// </remarks> | ||
void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/IHostApplicationBuilderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting.Fakes; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Console; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests; | ||
|
||
public class IHostApplicationBuilderTests | ||
{ | ||
[Fact] | ||
public void TestIHostApplicationBuilderCanBeUsedInExtensionMethod() | ||
{ | ||
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(new HostApplicationBuilderSettings | ||
{ | ||
EnvironmentName = "Development" | ||
}); | ||
|
||
builder.VerifyBuilderWorks(); | ||
|
||
using IHost host = builder.Build(); | ||
|
||
// VerifyBuilderWorks should have configured a FakeServiceProviderFactory with the following State. | ||
FakeServiceCollection fakeServices = host.Services.GetRequiredService<FakeServiceCollection>(); | ||
Assert.Equal("Hi!", fakeServices.State); | ||
} | ||
} | ||
|
||
internal static class HostBuilderExtensions | ||
{ | ||
public static void VerifyBuilderWorks(this IHostApplicationBuilder builder) | ||
{ | ||
var propertyKey = typeof(HostBuilderExtensions); | ||
builder.Properties[propertyKey] = 3; | ||
Assert.Equal(3, builder.Properties[propertyKey]); | ||
|
||
Assert.Equal(1, builder.Configuration.GetChildren().Count()); | ||
Assert.Equal(2, builder.Configuration.Sources.Count); // there's an empty source by default | ||
Assert.Equal("Development", builder.Configuration[HostDefaults.EnvironmentKey]); | ||
|
||
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string> | ||
{ | ||
["Key1"] = "value1" | ||
}); | ||
|
||
Assert.Equal(2, builder.Configuration.GetChildren().Count()); | ||
Assert.Equal(3, builder.Configuration.Sources.Count); | ||
Assert.Equal("value1", builder.Configuration["Key1"]); | ||
Assert.Null(builder.Configuration["Key2"]); | ||
|
||
Assert.True(builder.Environment.IsDevelopment()); | ||
Assert.NotNull(builder.Environment.ContentRootFileProvider); | ||
|
||
Assert.DoesNotContain(builder.Services, sd => sd.ImplementationType == typeof(ConsoleLoggerProvider)); | ||
builder.Logging.AddConsole(); | ||
Assert.Contains(builder.Services, sd => sd.ImplementationType == typeof(ConsoleLoggerProvider)); | ||
|
||
builder.Services.AddSingleton(typeof(IHostApplicationBuilderTests)); | ||
|
||
builder.ConfigureContainer(new FakeServiceProviderFactory(), container => container.State = "Hi!"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.