#20527: Extending DefaultPlatformConfigurationAccessor to check for setting in ConnectionStrings and AppSettings

Work Item: 20527
This commit is contained in:
Lombiq
2014-03-07 02:40:37 +01:00
committed by nightwolf226
parent 852d08a3f1
commit 438483e502
3 changed files with 15 additions and 6 deletions

View File

@@ -90,6 +90,7 @@
<HintPath>..\..\..\..\lib\nhibernate\NHibernate.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Web" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.Web.Entity" />

View File

@@ -1,21 +1,29 @@
using Microsoft.WindowsAzure;
using System.Configuration;
namespace Orchard.Azure.Services.Environment.Configuration {
public class DefaultPlatformConfigurationAccessor : IPlatformConfigurationAccessor {
/// <summary>
/// Reads a setting from platform configuration, trying first prefixed with the current tenant name and
/// secondly with no prefix.
/// Trying to read a setting from the following sources in the following order (with and then without tenant name prefix):
/// CloudConfigurationManager, ConnectionStrings, AppSettings.
/// </summary>
/// <param name="name">The name of the setting to read.</param>
/// <param name="tenant">The curren tenant's name.</param>
/// <param name="tenant">The current tenant's name.</param>
/// <param name="namePrefix">An optional prefix to prepend the setting name with.</param>
/// <returns>The value of the setting if found with or without tenant name prefix, otherwise null.</returns>
/// <returns>The value of the setting if found in any of the available sources, otherwise null.</returns>
public string GetSetting(string name, string tenant, string namePrefix = null) {
var tenantName = tenant + ":" + (namePrefix ?? string.Empty) + name;
var fallbackName = (namePrefix ?? string.Empty) + name;
return CloudConfigurationManager.GetSetting(tenantName) ?? CloudConfigurationManager.GetSetting(fallbackName);
var settingFromCloudConfiguration = CloudConfigurationManager.GetSetting(tenantName) ?? CloudConfigurationManager.GetSetting(fallbackName);
if (!string.IsNullOrEmpty(settingFromCloudConfiguration)) return settingFromCloudConfiguration;
var settingFromConnectionStrings = ConfigurationManager.ConnectionStrings[tenantName] ?? ConfigurationManager.ConnectionStrings[fallbackName];
if (settingFromConnectionStrings != null) return settingFromConnectionStrings.ConnectionString;
return ConfigurationManager.AppSettings[tenantName] ?? ConfigurationManager.AppSettings[fallbackName];
}
}
}

View File

@@ -6,7 +6,7 @@
/// Reads a setting using the available implementation(s).
/// </summary>
/// <param name="name">The name of the setting to read.</param>
/// <param name="tenant">The curren tenant's name.</param>
/// <param name="tenant">The current tenant's name.</param>
/// <param name="namePrefix">An optional prefix to prepend the setting name with.</param>
/// <returns>The value of the setting if found with or without tenant name prefix, otherwise null.</returns>
/// <see cref="DefaultPlatformConfigurationAccessor" />