diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Orchard.Azure.csproj b/src/Orchard.Web/Modules/Orchard.Azure/Orchard.Azure.csproj index e562191ea..a37eca615 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Orchard.Azure.csproj +++ b/src/Orchard.Web/Modules/Orchard.Azure/Orchard.Azure.csproj @@ -103,7 +103,6 @@ - diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs index 89711d7f2..59aa1b1c8 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/CacheClientConfiguration.cs @@ -6,11 +6,11 @@ namespace Orchard.Azure.Services.Caching { public class CacheClientConfiguration { - public static CacheClientConfiguration FromPlatformConfiguration(string tenant, string settingNamePrefix) { + public static CacheClientConfiguration FromPlatformConfiguration(string tenant, string settingNamePrefix, IPlatformConfigurationAccessor pca) { return new CacheClientConfiguration { - HostIdentifier = PlatformConfiguration.GetSetting(Constants.CacheHostIdentifierSettingName, tenant, settingNamePrefix), - CacheName = PlatformConfiguration.GetSetting(Constants.CacheCacheNameSettingName, tenant, settingNamePrefix), - AuthorizationToken = PlatformConfiguration.GetSetting(Constants.CacheAuthorizationTokenSettingName, tenant, settingNamePrefix), + HostIdentifier = pca.GetSetting(Constants.CacheHostIdentifierSettingName, tenant, settingNamePrefix), + CacheName = pca.GetSetting(Constants.CacheCacheNameSettingName, tenant, settingNamePrefix), + AuthorizationToken = pca.GetSetting(Constants.CacheAuthorizationTokenSettingName, tenant, settingNamePrefix), }; } diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs index ecfe6084d..ee5b97865 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheProvider.cs @@ -1,7 +1,8 @@ -using Microsoft.ApplicationServer.Caching; -using NHibernate.Cache; using System; using System.Collections.Generic; +using Microsoft.ApplicationServer.Caching; +using NHibernate.Cache; +using Orchard.Azure.Services.Environment.Configuration; namespace Orchard.Azure.Services.Caching.Database { @@ -39,7 +40,8 @@ namespace Orchard.Azure.Services.Caching.Database { if (properties.TryGetValue("compression_enabled", out enableCompressionString)) enableCompression = Boolean.Parse(enableCompressionString); - configuration = CacheClientConfiguration.FromPlatformConfiguration(tenantName, Constants.DatabaseCacheSettingNamePrefix); + var pca = new DefaultPlatformConfigurationAccessor(); + configuration = CacheClientConfiguration.FromPlatformConfiguration(tenantName, Constants.DatabaseCacheSettingNamePrefix, pca); configuration.CompressionIsEnabled = enableCompression; configuration.Validate(); } diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs index 66629ae2a..bf994e216 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using Microsoft.ApplicationServer.Caching; using Orchard.Azure.Services.Environment.Configuration; @@ -8,17 +9,17 @@ using Orchard.Environment.Extensions; using Orchard.Logging; using Orchard.OutputCache.Models; using Orchard.OutputCache.Services; -using System.Globalization; namespace Orchard.Azure.Services.Caching.Output { [OrchardFeature(Constants.OutputCacheFeatureName)] [OrchardSuppressDependency("Orchard.OutputCache.Services.DefaultCacheStorageProvider")] public class AzureOutputCacheStorageProvider : Component, IOutputCacheStorageProvider { + private readonly DataCache _cache; private readonly string _regionAlphaNumeric; - public AzureOutputCacheStorageProvider(ShellSettings shellSettings, IAzureOutputCacheHolder cacheHolder) { + public AzureOutputCacheStorageProvider(ShellSettings shellSettings, IAzureOutputCacheHolder cacheHolder, IPlatformConfigurationAccessor pca) { var region = shellSettings.Name; @@ -28,12 +29,11 @@ namespace Orchard.Azure.Services.Caching.Output { // of two distinct original region strings yielding the same transformed region string. _regionAlphaNumeric = new String(Array.FindAll(region.ToCharArray(), Char.IsLetterOrDigit)) + region.GetHashCode().ToString(CultureInfo.InvariantCulture); - _cache = cacheHolder.TryGetDataCache(() => { CacheClientConfiguration cacheConfig; try { - cacheConfig = CacheClientConfiguration.FromPlatformConfiguration(shellSettings.Name, Constants.OutputCacheSettingNamePrefix); + cacheConfig = CacheClientConfiguration.FromPlatformConfiguration(shellSettings.Name, Constants.OutputCacheSettingNamePrefix, pca); cacheConfig.Validate(); } catch (Exception ex) { diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/DefaultPlatformConfigurationAccessor.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/DefaultPlatformConfigurationAccessor.cs index cdfa41a52..5c74f9a6f 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/DefaultPlatformConfigurationAccessor.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/DefaultPlatformConfigurationAccessor.cs @@ -1,20 +1,36 @@ using Microsoft.WindowsAzure; using System.Configuration; +using System; namespace Orchard.Azure.Services.Environment.Configuration { + /// + /// Provides a default IPlatformConfigurationAccessor implementation that reads configuration settings + /// from cloud service role configuration, app settings and connection strings. + /// + /// + /// Settings are read first using the CloudConfigurationManager class, which looks first in cloud service role + /// configuration if running in a Windows Azure Cloud Service and secondly in app settings (either in Web.config or in + /// Windows Azure Web Site app settings configuration). If the setting is not found using CloudConfigurationManager + /// then connection strings (either in Web.config or in Windows Azure Web Site connection strings configuration) is + /// checked. Both the tenant-specific name and the tenant-neutral name are checked within each configuration source + /// before proceeding to the next one. + /// public class DefaultPlatformConfigurationAccessor : IPlatformConfigurationAccessor { - /// - /// Trying to read a setting using the default implementation. - /// - /// The name of the setting to read. - /// The current tenant's name. - /// An optional prefix to prepend the setting name with. - /// The value of the setting if found in any of the available sources, otherwise null. - /// public string GetSetting(string name, string tenant, string namePrefix = null) { - return PlatformConfiguration.GetSetting(name, tenant, namePrefix); + var tenantName = String.Format("{0}:{1}{2}", tenant, namePrefix, name); + var fallbackName = String.Format("{0}{2}", namePrefix, name); + + var cloudConfigurationValue = CloudConfigurationManager.GetSetting(tenantName) ?? CloudConfigurationManager.GetSetting(fallbackName); + if (!String.IsNullOrEmpty(cloudConfigurationValue)) + return cloudConfigurationValue; + + var connectionStringValue = ConfigurationManager.ConnectionStrings[tenantName] ?? ConfigurationManager.ConnectionStrings[fallbackName]; + if (connectionStringValue != null) + return connectionStringValue.ConnectionString; + + return null; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/IPlatformConfigurationAccessor.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/IPlatformConfigurationAccessor.cs index 1b32d7589..3a30f317a 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/IPlatformConfigurationAccessor.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/IPlatformConfigurationAccessor.cs @@ -1,16 +1,17 @@ namespace Orchard.Azure.Services.Environment.Configuration { + /// + /// Represents a service for reading configuration settings from the underlying platform configuration. + /// public interface IPlatformConfigurationAccessor : IDependency { /// - /// Reads a setting using the available implementation(s). - /// Implementations other than DefaultPlatformConfigurationAccessor do not have any effect on the behavior of caching services. + /// Reads a configuration setting from the underlying platform configuration. /// /// The name of the setting to read. /// The current tenant's name. /// An optional prefix to prepend the setting name with. /// The value of the setting if found with or without tenant name prefix, otherwise null. - /// string GetSetting(string name, string tenant, string namePrefix); } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/PlatformConfiguration.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/PlatformConfiguration.cs deleted file mode 100644 index b52773685..000000000 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/PlatformConfiguration.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.WindowsAzure; -using System.Configuration; - -namespace Orchard.Azure.Services.Environment.Configuration { - - public static class PlatformConfiguration { - - /// - /// Trying to read a setting from the following sources in the following order (with and then without tenant name prefix): - /// CloudConfigurationManager, ConnectionStrings, AppSettings. - /// - /// The name of the setting to read. - /// The current tenant's name. - /// An optional prefix to prepend the setting name with. - /// The value of the setting if found in any of the available sources, otherwise null. - public static string GetSetting(string name, string tenant, string namePrefix = null) { - var tenantName = tenant + ":" + (namePrefix ?? string.Empty) + name; - var fallbackName = (namePrefix ?? string.Empty) + name; - - 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]; - } - } -} \ No newline at end of file