Skip to content

Commit

Permalink
added ConfigurationManager configuration source and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado committed Jul 11, 2021
1 parent fac69a9 commit 35c0a94
Show file tree
Hide file tree
Showing 18 changed files with 790 additions and 52 deletions.
32 changes: 31 additions & 1 deletion Microsoft.Extensions.AspNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2355A742-ABD
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "demo", "demo", "{C059CCE4-C3EC-4CC2-B7F6-AD4FABBAFB2F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{85C121D2-EBA5-4580-A375-4383371885A0}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{85C121D2-EBA5-4580-A375-4383371885A0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".root", ".root", "{3CDB20C4-5D12-40D9-96D3-2328C4F46583}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -52,6 +52,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".info", ".info", "{BE18BA32
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Extensions.Configuration.ConfigurationManager", "src\Microsoft.Extensions.Configuration.ConfigurationManager\Microsoft.Extensions.Configuration.ConfigurationManager.csproj", "{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Configuration.ConfigurationManager.Tests", "tests\Microsoft.Extensions.Configuration.ConfigurationManager.Tests\Microsoft.Extensions.Configuration.ConfigurationManager.Tests.csproj", "{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -170,6 +174,30 @@ Global
{989ADF77-2743-4073-8295-A12720613E85}.Release|x64.Build.0 = Release|Any CPU
{989ADF77-2743-4073-8295-A12720613E85}.Release|x86.ActiveCfg = Release|Any CPU
{989ADF77-2743-4073-8295-A12720613E85}.Release|x86.Build.0 = Release|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Debug|x64.ActiveCfg = Debug|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Debug|x64.Build.0 = Debug|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Debug|x86.ActiveCfg = Debug|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Debug|x86.Build.0 = Debug|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Release|Any CPU.Build.0 = Release|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Release|x64.ActiveCfg = Release|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Release|x64.Build.0 = Release|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Release|x86.ActiveCfg = Release|Any CPU
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D}.Release|x86.Build.0 = Release|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Debug|x64.ActiveCfg = Debug|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Debug|x64.Build.0 = Debug|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Debug|x86.ActiveCfg = Debug|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Debug|x86.Build.0 = Debug|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Release|Any CPU.Build.0 = Release|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Release|x64.ActiveCfg = Release|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Release|x64.Build.0 = Release|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Release|x86.ActiveCfg = Release|Any CPU
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -185,6 +213,8 @@ Global
{A8D9A522-0301-4730-A2ED-8F082961012C} = {2355A742-ABD4-402A-8040-A46B7D88BAB5}
{989ADF77-2743-4073-8295-A12720613E85} = {85C121D2-EBA5-4580-A375-4383371885A0}
{F29821A2-9312-4ABF-AC2C-D06D3AF67919} = {A4274275-5F57-4EC3-9651-AD4BCFB0E488}
{EB4BBFE9-108E-48C4-AF64-2806CD96E06D} = {2355A742-ABD4-402A-8040-A46B7D88BAB5}
{D6BB544A-7A32-4C2B-A5BA-3DC6A5C0CA5E} = {85C121D2-EBA5-4580-A375-4383371885A0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {20512F05-6FBC-47B7-BBE8-949744341E3D}
Expand Down
7 changes: 6 additions & 1 deletion demo/SampleWebApplication/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.SystemWeb;
using Microsoft.Extensions.Configuration.ConfigurationManager;
using SampleWebApplication.App_Start;

namespace SampleWebApplication
Expand All @@ -12,13 +13,17 @@ void Application_Start(object sender, EventArgs e)
SystemWebHosting.ConfigureHost(builder =>
{
// configure host here.

builder
.ConfigureAppConfiguration(((_, configurationBuilder) => configurationBuilder.AddConfigurationManager()));
});

SystemWebHosting.ConfigureWebHost(builder =>
{
// configure web host here.

builder.UseStartup<Startup>();
builder
.UseStartup<Startup>();
});
}
}
Expand Down
8 changes: 8 additions & 0 deletions demo/SampleWebApplication/SampleWebApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@
<Service Include="{4A0DDDB5-7A95-4FBF-97CC-616D07737A77}" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNet.Hosting.SystemWeb.Abstractions\Microsoft.AspNet.Hosting.SystemWeb.Abstractions.csproj">
<Project>{a8d9a522-0301-4730-a2ed-8f082961012c}</Project>
<Name>Microsoft.AspNet.Hosting.SystemWeb.Abstractions</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Microsoft.AspNet.Hosting.SystemWeb.Mvc.Abstractions\Microsoft.AspNet.Hosting.SystemWeb.Mvc.Abstractions.csproj">
<Project>{c23cd5e0-bdd7-43a1-8833-95ff1c86ecad}</Project>
<Name>Microsoft.AspNet.Hosting.SystemWeb.Mvc.Abstractions</Name>
Expand All @@ -431,6 +435,10 @@
<Project>{1b29ea7f-229d-463d-9227-4d7ae99b3ffa}</Project>
<Name>Microsoft.AspNet.Hosting.Abstractions</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.ConfigurationManager\Microsoft.Extensions.Configuration.ConfigurationManager.csproj">
<Project>{eb4bbfe9-108e-48c4-af64-2806cd96e06d}</Project>
<Name>Microsoft.Extensions.Configuration.ConfigurationManager</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
Expand Down
2 changes: 1 addition & 1 deletion demo/SampleWebApplication/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="Environment" value="Development" />
<add key="dotnet:Environment" value="Development" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Web.Compilation;
using System.Web.Configuration;
using Microsoft.AspNet.Hosting.SystemWeb.Builder;
using Microsoft.AspNet.Hosting.SystemWeb.DependencyInjection;
using Microsoft.AspNet.Hosting.SystemWeb.Startup;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.ConfigurationManager;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using SystemWebHostingEnvironment = System.Web.Hosting.HostingEnvironment;

namespace Microsoft.AspNet.Hosting.SystemWeb
{
Expand All @@ -32,15 +31,19 @@ public GenericWebHostBuilder(IHostBuilder builder, GenericWebHostBuilderOptions
{
this.builder = builder;

var configBuilder = new ConfigurationBuilder()
.AddInMemoryCollection(GetAppSettings(options.SuppressEnvironmentConfiguration));
var configBuilder = new ConfigurationBuilder();

if (!options.SuppressEnvironmentConfiguration)
{
configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
configBuilder.AddEnvironmentVariables(prefix: "ASPNET_");
}

if (!options.SuppressConfigurationConfiguration)
{
configBuilder.AddConfigurationManager(prefix: "aspnet:", skipConnectionStrings: true);
}

config = configBuilder.Build();

this.builder.ConfigureHostConfiguration(config =>
Expand Down Expand Up @@ -200,48 +203,18 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
throw new NotImplementedException();
}

private static IEnumerable<KeyValuePair<string, string>> GetAppSettings(bool suppressEnvironmentConfiguration)
{
if (suppressEnvironmentConfiguration)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
}

var appSettings = WebConfigurationManager.AppSettings;

if (appSettings.Count == 0)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
}

return GetEnumerable(appSettings);

IEnumerable<KeyValuePair<string, string>> GetEnumerable(System.Collections.Specialized.NameValueCollection settings)
{
foreach (var key in settings.AllKeys)
{
if (key.StartsWith("Microsoft:AspNet:Hosting:HttpRuntime:", StringComparison.OrdinalIgnoreCase)
&& settings.GetValues(key) is string[] values && values.Length > 0)
{
yield return new KeyValuePair<string, string>(key.Substring(37), values[values.Length - 1]);
}
}

}
}

private static WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext context)
{
if (!context.Properties.TryGetValue(typeof(WebHostBuilderContext), out var contextVal))
{
var options = new WebHostOptions(context.Configuration, SystemWebHostingEnvironment.SiteName ?? string.Empty);
var options = new WebHostOptions(context.Configuration, System.Web.Hosting.HostingEnvironment.SiteName ?? string.Empty);
HostingEnvironment hostingEnvironment = new HostingEnvironment();
var webHostBuilderContext = new WebHostBuilderContext
{
Configuration = context.Configuration,
HostingEnvironment = hostingEnvironment,
};
hostingEnvironment.Initialize(System.Web.HttpRuntime.AppDomainAppPath, options);
hostingEnvironment.Initialize(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, options);
context.Properties[typeof(WebHostBuilderContext)] = webHostBuilderContext;
context.Properties[typeof(WebHostOptions)] = options;
return webHostBuilderContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.AspNet.Hosting.SystemWeb
using System.Configuration;

namespace Microsoft.AspNet.Hosting.SystemWeb
{
/// <summary>
/// Builder options for use with ConfigureWebHost.
Expand All @@ -12,9 +14,9 @@ public class GenericWebHostBuilderOptions
public bool SuppressEnvironmentConfiguration { get; set; }

/// <summary>
/// Indicates if "Microsoft:AspNet:Hosting:HttpRuntime:" prefixed <see cref="WebConfigurationManager.AppSettings"/> should be added to configuration.
/// They are added by default.
/// Gets or sets a value indicating whether "aspnet:" prefixed <see cref="ConfigurationManager.AppSettings"/>.
/// </summary>
public bool SuppressAppSettingsConfiguration { get; set; }
/// <value><see langword="true" /> if [suppress configuration configuration]; otherwise, <see langword="false" />.</value>
public bool SuppressConfigurationConfiguration { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="System.Configuration" />
<Reference Include="System.Web" />
</ItemGroup>

Expand All @@ -26,6 +27,7 @@
<ItemGroup>
<ProjectReference Include="..\Microsoft.AspNet.Hosting.Abstractions\Microsoft.AspNet.Hosting.Abstractions.csproj" />
<ProjectReference Include="..\Microsoft.AspNet.Hosting.SystemWeb.Abstractions\Microsoft.AspNet.Hosting.SystemWeb.Abstractions.csproj" />
<ProjectReference Include="..\Microsoft.Extensions.Configuration.ConfigurationManager\Microsoft.Extensions.Configuration.ConfigurationManager.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Specialized;
using System.Web.Configuration;
using Microsoft.AspNet.Hosting.SystemWeb.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration.ConfigurationManager;
using Microsoft.Extensions.Hosting;

namespace Microsoft.AspNet.Hosting.SystemWeb
Expand All @@ -20,13 +19,9 @@ public static IHostBuilder ConfigureSystemWebHostDefaults(this IHostBuilder buil
}

return builder
.UseContentRoot(System.Web.HttpRuntime.AppDomainAppPath)
.UseContentRoot(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath)
.ConfigureHostConfiguration(configurationBuilder =>
configurationBuilder.AddInMemoryCollection(
from key in WebConfigurationManager.AppSettings.AllKeys
let value = WebConfigurationManager.AppSettings.GetValues(key)?.LastOrDefault()
where !(value is null)
select new KeyValuePair<string, string>(key, value)))
configurationBuilder.AddConfigurationManager(prefix: "dotnet:", skipConnectionStrings: true))
.UseServiceProviderFactory(new SystemWebServiceProviderFactory());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System;
using System.Configuration;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.Configuration.ConfigurationManager
{
/// <summary>
/// A <see cref="System.Configuration.ConfigurationManager" /> based <see cref="ConfigurationProvider" />.
/// </summary>
public class ConfigurationManagerConfigurationProvider : ConfigurationProvider
{
//private readonly System.Configuration.ConfigurationManager configurationManager;
/// <summary>
/// The prefix
/// </summary>
private string prefix;

/// <summary>
/// The skip connection strings
/// </summary>
private bool skipConnectionStrings;

/// <summary>
/// The application settings enumeration mode
/// </summary>
private NameValueCollectionEnumerationMode appSettingsEnumerationMode;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationManagerConfigurationProvider" /> class.
/// </summary>
/// <param name="prefix">The prefix.</param>
/// <param name="skipConnectionStrings">if set to <see langword="true" /> skip loading <see cref="System.Configuration.ConfigurationManager.ConnectionStrings" />.</param>
/// <param name="appSettingsEnumerationMode">The application settings enumeration mode.</param>
public ConfigurationManagerConfigurationProvider(string prefix, bool skipConnectionStrings, NameValueCollectionEnumerationMode appSettingsEnumerationMode)
{
this.prefix = prefix;
this.skipConnectionStrings = skipConnectionStrings;
this.appSettingsEnumerationMode = appSettingsEnumerationMode;
}

/// <inheritdoc/>
public override void Load()
{
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

foreach (var pair in System.Configuration.ConfigurationManager.AppSettings.ToKeyValuePairEnumerable(this.appSettingsEnumerationMode, this.prefix))
{
data.Add(pair.Key, pair.Value);
}

if (!this.skipConnectionStrings)
{
foreach (var pair in System.Configuration.ConfigurationManager.ConnectionStrings.ToKeyValuePairEnumerable())
{
data.Add(pair.Key, pair.Value);
}
}

this.Data = data;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Microsoft.Extensions.Configuration.ConfigurationManager
{
/// <summary>
/// Represents a <see cref="ConfigurationManager"/> as an <see cref="IConfigurationSource"/>.
/// </summary>
public class ConfigurationManagerConfigurationSource : IConfigurationSource
{
/// <summary>
/// A prefix used to filter <see cref="System.Configuration.ConfigurationManager.AppSettings" />.
/// </summary>
/// <value>The prefix.</value>
public string Prefix { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to skip loading <see cref="System.Configuration.ConfigurationManager.ConnectionStrings" />.
/// </summary>
/// <value><see langword="true" /> if skip loading <see cref="System.Configuration.ConfigurationManager.ConnectionStrings" />; otherwise, <see langword="false" />.</value>
public bool SkipConnectionStrings { get; set; }

/// <summary>
/// Gets or sets the <see cref="System.Configuration.ConfigurationManager.AppSettings" /> <see cref="NameValueCollectionEnumerationMode">enumeration mode</see>.
/// </summary>
/// <value>The <see cref="System.Configuration.ConfigurationManager.AppSettings" /> <see cref="NameValueCollectionEnumerationMode">enumeration mode</see>.</value>
public NameValueCollectionEnumerationMode AppSettingsEnumerationMode { get; set; }

/// <summary>
/// Builds the <see cref="ConfigurationManagerConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>The <see cref="ConfigurationManagerConfigurationProvider"/> built for this source.</returns>
public IConfigurationProvider Build(IConfigurationBuilder builder)
=> new ConfigurationManagerConfigurationProvider(this.Prefix, this.SkipConnectionStrings, this.AppSettingsEnumerationMode);
}
}
Loading

0 comments on commit 35c0a94

Please sign in to comment.