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

Add ConfigureAspNetCoreDefaults #118

Closed
wants to merge 9 commits into from
Closed
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: 8 additions & 2 deletions MetaPackages.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26412.1
VisualStudioVersion = 15.0.26507.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}"
EndProject
Expand Down Expand Up @@ -46,6 +46,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimDeps", "tools\TrimDeps\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInjectionApp", "test\TestSites\DependencyInjectionApp\DependencyInjectionApp.csproj", "{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.All.Tests", "test\Microsoft.AspNetCore.All.Tests\Microsoft.AspNetCore.All.Tests.csproj", "{914D3FAD-8B94-4353-B24F-B264F675481C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -100,6 +102,10 @@ Global
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.Build.0 = Release|Any CPU
{914D3FAD-8B94-4353-B24F-B264F675481C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{914D3FAD-8B94-4353-B24F-B264F675481C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{914D3FAD-8B94-4353-B24F-B264F675481C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{914D3FAD-8B94-4353-B24F-B264F675481C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -118,7 +124,7 @@ Global
{AE4216BF-D471-471B-82F3-6B6D004F7D17} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}
{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3} = {EC22261D-0DE1-47DE-8F7C-072675D6F5B4}
{914D3FAD-8B94-4353-B24F-B264F675481C} = {9E49B5B9-9E72-42FB-B684-90CA1B1BCF9C}
EndGlobalSection
EndGlobal
32 changes: 32 additions & 0 deletions src/Microsoft.AspNetCore/AspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options.Infrastructure;

namespace Microsoft.AspNetCore
{
/// <summary>
/// Exposes extension method for establishing configuration defaults.
/// </summary>
public static class AspNetCoreExtensions
{
/// <summary>
/// Allows features to do some default setup for their options, i.e. binding against the default configuration.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to modify.</param>
/// <returns>The service collection.</returns>
public static IServiceCollection ConfigureAspNetCoreDefaults(this IServiceCollection services)
{
services.AddTransient(typeof(IConfigureOptions<>), typeof(ConfigureDefaults<>));
services.AddTransient(typeof(IConfigureNamedOptions<>), typeof(ConfigureDefaults<>));
return services;
}
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.AspNetCore/KestrelServerOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options.Infrastructure;

namespace Microsoft.AspNetCore
{
internal class KestrelServerOptionsSetup : IConfigureOptions<KestrelServerOptions>
internal class KestrelServerOptionsSetup : ConfigureDefaultOptions<KestrelServerOptions>
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _configurationRoot;
Expand All @@ -29,7 +29,7 @@ public KestrelServerOptionsSetup(
_loggerFactory = loggerFactory;
}

public void Configure(KestrelServerOptions options)
public override void Configure(string name, KestrelServerOptions options)
{
BindConfiguration(options);
}
Expand All @@ -38,7 +38,7 @@ private void BindConfiguration(KestrelServerOptions options)
{
var certificateLoader = new CertificateLoader(_configurationRoot.GetSection("Certificates"), _loggerFactory, _hostingEnvironment.EnvironmentName);

foreach (var endPoint in _configurationRoot.GetSection("Kestrel:EndPoints").GetChildren())
foreach (var endPoint in _configurationRoot.GetSection("Microsoft:AspNetCore:Hosting:Kestrel:EndPoints").GetChildren())
{
BindEndPoint(options, endPoint, certificateLoader);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNetCore/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options.Infrastructure;

namespace Microsoft.AspNetCore
{
Expand Down Expand Up @@ -188,7 +188,7 @@ public static IWebHostBuilder CreateDefaultBuilder(string[] args)
})
.ConfigureServices(services =>
{
services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
services.AddTransient<ConfigureDefaultOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
});

return builder;
Expand Down
69 changes: 69 additions & 0 deletions test/Microsoft.AspNetCore.All.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) .NET Foundation. All rights reserved. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options.Infrastructure;
using Xunit;

namespace Microsoft.AspNetCore.Tests
{
public class ConfigurationTests
{
private class TestOptions
{
public string Message { get; set; }
}

private class ConfigureTestDefault : ConfigureDefaultOptions<TestOptions>
{
public ConfigureTestDefault(IConfiguration config) :
base(options => config.GetSection("Test").Bind(options))
{ }
}

[Fact]
public void ConfigureAspNetCoreDefaultsEnablesBindAgainstDefaultConfig()
{
var dic = new Dictionary<string, string>
{
{ "Test:Message", "yadayada"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();
var services = new ServiceCollection()
.AddSingleton<IConfiguration>(config)
.AddOptions()
.AddTransient<ConfigureDefaultOptions<TestOptions>, ConfigureTestDefault>()
.ConfigureAspNetCoreDefaults();
var sp = services.BuildServiceProvider();

var options = sp.GetRequiredService<IOptions<TestOptions>>().Value;
Assert.Equal("yadayada", options.Message);
}

[Fact]
public void DefaultConfigIgnoredWithoutConfigureAspNetCoreDefaults()
{
var dic = new Dictionary<string, string>
{
{ "Test:Message", "yadayada"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();
var services = new ServiceCollection()
.AddSingleton<IConfiguration>(config)
.AddOptions()
.AddTransient<ConfigureDefaultOptions<TestOptions>, ConfigureTestDefault>();
var sp = services.BuildServiceProvider();

var options = sp.GetRequiredService<IOptions<TestOptions>>().Value;
Assert.Null(options.Message);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\build\common.props" />

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.All\Microsoft.AspNetCore.All.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
</ItemGroup>

</Project>
58 changes: 38 additions & 20 deletions test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ public async Task BindsKestrelHttpEndPointFromConfiguration(string endPointAddre
{
File.WriteAllText("appsettings.json", @"
{
""Kestrel"": {
""EndPoints"": {
""EndPoint"": {
""Address"": """ + endPointAddress + @""",
""Port"": 0
""Microsoft"": {
""AspNetCore"": {
""Hosting"": {
""Kestrel"": {
""EndPoints"": {
""EndPoint"": {
""Address"": """ + endPointAddress + @""",
""Port"": 0
}
}
}
}
}
}
Expand Down Expand Up @@ -142,12 +148,18 @@ public async Task BindsKestrelHttpsEndPointFromConfiguration_ReferencedCertifica
{
File.WriteAllText("appsettings.json", @"
{
""Kestrel"": {
""EndPoints"": {
""EndPoint"": {
""Address"": ""127.0.0.1"",
""Port"": 0,
""Certificate"": ""TestCert""
""Microsoft"": {
""AspNetCore"": {
""Hosting"": {
""Kestrel"": {
""EndPoints"": {
""EndPoint"": {
""Address"": ""127.0.0.1"",
""Port"": 0,
""Certificate"": ""TestCert""
}
}
}
}
}
},
Expand Down Expand Up @@ -181,15 +193,21 @@ public async Task BindsKestrelHttpsEndPointFromConfiguration_InlineCertificateFi
{
File.WriteAllText("appsettings.json", @"
{
""Kestrel"": {
""EndPoints"": {
""EndPoint"": {
""Address"": ""127.0.0.1"",
""Port"": 0,
""Certificate"": {
""Source"": ""File"",
""Path"": ""testCert.pfx"",
""Password"": ""testPassword""
""Microsoft"": {
""AspNetCore"": {
""Hosting"": {
""Kestrel"": {
""EndPoints"": {
""EndPoint"": {
""Address"": ""127.0.0.1"",
""Port"": 0,
""Certificate"": {
""Source"": ""File"",
""Path"": ""testCert.pfx"",
""Password"": ""testPassword""
}
}
}
}
}
}
Expand Down