-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IHostApplicationBuilder interface (#86974)
* Add IHostApplicationBuilder and implement it in HostApplicationBuilder. - Move ILoggingBuilder from MS.Ext.Logging to MS.Ext.Logging.Abstractions so IHostApplicationBuilder can reference it. - Add IConfigurationManager and implement it in ConfigurationManager. - Add CompatiibilitySuppressions. These errors are caused by ILoggingBuilder being moved to Logging.Abstractions, and ApiCompat not respecting TypeForwardedTo attributes. Fix #85486
- Loading branch information
Showing
19 changed files
with
232 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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.