Skip to content
This repository was archived by the owner on Nov 21, 2018. It is now read-only.

Commit 4b18cf5

Browse files
committed
Add ConfigureAspNetCoreDefaults
1 parent a99d1d9 commit 4b18cf5

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

MetaPackages.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
3-
VisualStudioVersion = 15.0.26412.1
3+
VisualStudioVersion = 15.0.26507.0
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}"
66
EndProject
@@ -46,6 +46,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimDeps", "tools\TrimDeps\
4646
EndProject
4747
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInjectionApp", "test\TestSites\DependencyInjectionApp\DependencyInjectionApp.csproj", "{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}"
4848
EndProject
49+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Tests", "test\Microsoft.AspNetCore.Tests\Microsoft.AspNetCore.Tests.csproj", "{914D3FAD-8B94-4353-B24F-B264F675481C}"
50+
EndProject
4951
Global
5052
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5153
Debug|Any CPU = Debug|Any CPU
@@ -100,6 +102,10 @@ Global
100102
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
101103
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
102104
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.Build.0 = Release|Any CPU
105+
{914D3FAD-8B94-4353-B24F-B264F675481C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
106+
{914D3FAD-8B94-4353-B24F-B264F675481C}.Debug|Any CPU.Build.0 = Debug|Any CPU
107+
{914D3FAD-8B94-4353-B24F-B264F675481C}.Release|Any CPU.ActiveCfg = Release|Any CPU
108+
{914D3FAD-8B94-4353-B24F-B264F675481C}.Release|Any CPU.Build.0 = Release|Any CPU
103109
EndGlobalSection
104110
GlobalSection(SolutionProperties) = preSolution
105111
HideSolutionNode = FALSE
@@ -118,7 +124,7 @@ Global
118124
{AE4216BF-D471-471B-82F3-6B6D004F7D17} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
119125
{302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
120126
{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
121-
{302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
122127
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3} = {EC22261D-0DE1-47DE-8F7C-072675D6F5B4}
128+
{914D3FAD-8B94-4353-B24F-B264F675481C} = {9E49B5B9-9E72-42FB-B684-90CA1B1BCF9C}
123129
EndGlobalSection
124130
EndGlobal
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.DependencyInjection.Extensions;
10+
using Microsoft.Extensions.Options;
11+
using Microsoft.Extensions.Options.Infrastructure;
12+
13+
namespace Microsoft.AspNetCore
14+
{
15+
/// <summary>
16+
/// Exposes extension method for establishing configuration defaults.
17+
/// </summary>
18+
public static class AspNetCoreExtensions
19+
{
20+
/// <summary>
21+
/// Allows features to do some default setup for their options, i.e. binding against the default configuration.
22+
/// </summary>
23+
/// <param name="services">The <see cref="IServiceCollection"/> to modify.</param>
24+
/// <returns>The service collection.</returns>
25+
public static IServiceCollection ConfigureAspNetCoreDefaults(this IServiceCollection services)
26+
{
27+
services.AddTransient(typeof(IConfigureOptions<>), typeof(ConfigureDefaults<>));
28+
services.AddTransient(typeof(IConfigureNamedOptions<>), typeof(ConfigureDefaults<>));
29+
return services;
30+
}
31+
}
32+
}

src/Microsoft.AspNetCore/WebHost.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.DependencyInjection;
1414
using Microsoft.Extensions.Logging;
1515
using Microsoft.Extensions.Options;
16+
using Microsoft.Extensions.Options.Infrastructure;
1617

1718
namespace Microsoft.AspNetCore
1819
{
@@ -124,6 +125,7 @@ private static IWebHost StartWith(string url, Action<IServiceCollection> configu
124125
/// load <see cref="IConfiguration"/> from environment variables,
125126
/// configures the <see cref="ILoggerFactory"/> to log to the console and debug output,
126127
/// enables IIS integration,
128+
/// enables the ability for frameworks to bind their options to their default configuration sections,
127129
/// and adds the developer exception page when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development'
128130
/// </remarks>
129131
/// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns>
@@ -143,6 +145,7 @@ public static IWebHostBuilder CreateDefaultBuilder() =>
143145
/// load <see cref="IConfiguration"/> from supplied command line args,
144146
/// configures the <see cref="ILoggerFactory"/> to log to the console and debug output,
145147
/// enables IIS integration,
148+
/// enables the ability for frameworks to bind their options to their default configuration sections,
146149
/// and adds the developer exception page when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development'
147150
/// </remarks>
148151
/// <param name="args">The command line args.</param>
@@ -189,6 +192,7 @@ public static IWebHostBuilder CreateDefaultBuilder(string[] args)
189192
.ConfigureServices(services =>
190193
{
191194
services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
195+
services.ConfigureAspNetCoreDefaults();
192196
});
193197

194198
return builder;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) .NET Foundation. All rights reserved. See License.txt in the project root for license information.
2+
3+
using System.Collections.Generic;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Options;
7+
using Microsoft.Extensions.Options.Infrastructure;
8+
using Xunit;
9+
10+
namespace Microsoft.AspNetCore.Tests
11+
{
12+
public class ConfigurationTests
13+
{
14+
private class TestOptions
15+
{
16+
public string Message { get; set; }
17+
}
18+
19+
private class ConfigureTestDefault : ConfigureDefaultOptions<TestOptions>
20+
{
21+
public ConfigureTestDefault(IConfiguration config) :
22+
base(options => config.GetSection("Test").Bind(options))
23+
{ }
24+
}
25+
26+
[Fact]
27+
public void ConfigureAspNetCoreDefaultsEnablesBindAgainstDefaultConfig()
28+
{
29+
var dic = new Dictionary<string, string>
30+
{
31+
{ "Test:Message", "yadayada"}
32+
};
33+
var configurationBuilder = new ConfigurationBuilder();
34+
configurationBuilder.AddInMemoryCollection(dic);
35+
var config = configurationBuilder.Build();
36+
var services = new ServiceCollection()
37+
.AddSingleton<IConfiguration>(config)
38+
.AddOptions()
39+
.AddTransient<ConfigureDefaultOptions<TestOptions>, ConfigureTestDefault>()
40+
.ConfigureAspNetCoreDefaults();
41+
var sp = services.BuildServiceProvider();
42+
43+
var options = sp.GetRequiredService<IOptions<TestOptions>>().Value;
44+
Assert.Equal("yadayada", options.Message);
45+
}
46+
47+
[Fact]
48+
public void DefaultConfigIgnoredWithoutConfigureAspNetCoreDefaults()
49+
{
50+
var dic = new Dictionary<string, string>
51+
{
52+
{ "Test:Message", "yadayada"}
53+
};
54+
var configurationBuilder = new ConfigurationBuilder();
55+
configurationBuilder.AddInMemoryCollection(dic);
56+
var config = configurationBuilder.Build();
57+
var services = new ServiceCollection()
58+
.AddSingleton<IConfiguration>(config)
59+
.AddOptions()
60+
.AddTransient<ConfigureDefaultOptions<TestOptions>, ConfigureTestDefault>();
61+
var sp = services.BuildServiceProvider();
62+
63+
var options = sp.GetRequiredService<IOptions<TestOptions>>().Value;
64+
Assert.Null(options.Message);
65+
}
66+
67+
}
68+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\build\common.props" />
4+
5+
<PropertyGroup>
6+
<TargetFramework>netcoreapp2.0</TargetFramework>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.All\Microsoft.AspNetCore.All.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
15+
<PackageReference Include="Moq" Version="$(MoqVersion)" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
17+
<PackageReference Include="xunit" Version="$(XunitVersion)" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)