Refactored platform configuration code in Orchard.Azure.

This commit is contained in:
Daniel Stolt
2014-03-26 00:03:10 +01:00
parent 4f4f1c870b
commit c58e82ebd1
7 changed files with 42 additions and 53 deletions

View File

@@ -103,7 +103,6 @@
<ItemGroup>
<Compile Include="Services\Environment\Configuration\DefaultPlatformConfigurationAccessor.cs" />
<Compile Include="Services\Environment\Configuration\IPlatformConfigurationAccessor.cs" />
<Compile Include="Services\Environment\Configuration\PlatformConfiguration.cs" />
<Compile Include="Services\TaskLease\AzureMachineNameProvider.cs" />
<Content Include="Web.config" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -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),
};
}

View File

@@ -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();
}

View File

@@ -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) {

View File

@@ -1,20 +1,36 @@
using Microsoft.WindowsAzure;
using System.Configuration;
using System;
namespace Orchard.Azure.Services.Environment.Configuration {
/// <summary>
/// Provides a default <c>IPlatformConfigurationAccessor</c> implementation that reads configuration settings
/// from cloud service role configuration, app settings and connection strings.
/// </summary>
/// <remarks>
/// Settings are read first using the <c>CloudConfigurationManager</c> 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.
/// </remarks>
public class DefaultPlatformConfigurationAccessor : IPlatformConfigurationAccessor {
/// <summary>
/// Trying to read a setting using the default implementation.
/// </summary>
/// <param name="name">The name of the setting to read.</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 in any of the available sources, otherwise null.</returns>
/// <see cref="PlatformConfiguration" />
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;
}
}
}

View File

@@ -1,16 +1,17 @@
namespace Orchard.Azure.Services.Environment.Configuration {
/// <summary>
/// Represents a service for reading configuration settings from the underlying platform configuration.
/// </summary>
public interface IPlatformConfigurationAccessor : IDependency {
/// <summary>
/// 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.
/// </summary>
/// <param name="name">The name of the setting to read.</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" />
string GetSetting(string name, string tenant, string namePrefix);
}
}

View File

@@ -1,29 +0,0 @@
using Microsoft.WindowsAzure;
using System.Configuration;
namespace Orchard.Azure.Services.Environment.Configuration {
public static class PlatformConfiguration {
/// <summary>
/// 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 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 in any of the available sources, otherwise null.</returns>
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];
}
}
}