Skip to content

Commit

Permalink
Merge pull request JimBobSquarePants#515 from joebyrne/develop
Browse files Browse the repository at this point in the history
#369 allow the user override settings in web.config app settings
  • Loading branch information
JimBobSquarePants authored Jan 30, 2018
2 parents 18ee2b4 + bdf9b5a commit 8dc54a4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void LoadGraphicsProcessors()
/// Returns the <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </summary>
/// <param name="name">
/// The name of the plugin to get the settings for.
/// The name of the plugin to get the settings for. Override settings by adding appsettings in web.config using the format ImageProcessor.&lt;.pluginname.&gt.&lt;settingKey&gt; e.g. 'ImageProcessor.GaussianBlur.MaxSize'. The key must exist in the config section for the appsetting to apply"
/// </param>
/// <returns>
/// The <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
Expand All @@ -276,6 +276,10 @@ private Dictionary<string, string> GetPluginSettings(string name)
settings = pluginElement.Settings
.Cast<SettingElement>()
.ToDictionary(setting => setting.Key, setting => setting.Value);

//override the settings found in config section with values in the app.config / deployment slot settings
OverrideDefaultSettingsWithAppSettingsValue(settings, name);

}
else
{
Expand Down Expand Up @@ -325,7 +329,8 @@ private void LoadImageServices()
}

/// <summary>
/// Returns the <see cref="SettingElementCollection"/> for the given plugin.
/// Returns the <see cref="SettingElementCollection"/> for the given plugin.
/// Override the settings using appSettings using the following format "ImageProcessor.&lt;PluginName&gt;.&lt;settingKey&gt; e.g. 'ImageProcessor.CloudImageService.Host'. The key must exist in the config section for the appsetting to apply"
/// </summary>
/// <param name="name">
/// The name of the plugin to get the settings for.
Expand All @@ -347,6 +352,9 @@ private Dictionary<string, string> GetServiceSettings(string name)
settings = serviceElement.Settings
.Cast<SettingElement>()
.ToDictionary(setting => setting.Key, setting => setting.Value);

//override the config section settings with values found in the app.config / deployment slot settings
OverrideDefaultSettingsWithAppSettingsValue(settings, name);
}
else
{
Expand Down Expand Up @@ -418,12 +426,42 @@ private void LoadImageCache()
this.ImageCacheSettings = cache.Settings
.Cast<SettingElement>()
.ToDictionary(setting => setting.Key, setting => setting.Value);

//override the settings found with values found in the app.config / deployment slot settings
OverrideDefaultSettingsWithAppSettingsValue(this.ImageCacheSettings, currentCache);

break;
}
}
}
}
#endregion

/// <summary>
/// Override the default settings discovered in the config sections, with settings stored in appsettings of app.config or deployment slot settings (if available)
/// This will allow the settings to be controlled per deployment slot within Microsoft Azure and similar services
/// The setting must exist in the config section to be overwritten by the appconfig values
/// </summary>
/// <param name="defaultSettings">The list of settings discovered in config section which will be modified with settings found in appSettings</param>
/// <param name="serviceOrPluginName">The name of the section, used to construct the appSetting key name</param>
private void OverrideDefaultSettingsWithAppSettingsValue(Dictionary<string, string> defaultSettings, string serviceOrPluginName)
{

Dictionary<string, string> copyOfSettingsForEnumeration = new Dictionary<string, string>(defaultSettings);
//for each default setting found in the config section
foreach (var setting in copyOfSettingsForEnumeration)
{
//check the app settings for a key in the sepcified format
string appSettingKeyName = "ImageProcessor." + serviceOrPluginName + "." + setting.Key;
if (!String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings[appSettingKeyName]))
{
//if the key is found in app settings use the app settings value rather than the value in the config section
defaultSettings[setting.Key] = System.Configuration.ConfigurationManager.AppSettings[appSettingKeyName];
}
}

}

#endregion
}
}
}
51 changes: 50 additions & 1 deletion tests/ImageProcessor.TestWebsite/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
null,
null,
null);

}
<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -178,6 +178,55 @@
<li>_dirMonSubdirs._dirMonCompletion = @dirMonSubdirsDirMonCompletion</li>
</ul>



<h3>Current settings</h3>
You can override the default values in the appSettings section of web.config. This also allows the values to be configured per deployment slot in Microsoft Azure
<table class="table-bordered">
<thead>
<tr>
<th>Setting</th>
<th>Value</th>
</tr>
</thead>

@{

//show the value used for a specific setting
ImageProcessor.Web.Configuration.ImageProcessorConfiguration ipc = ImageProcessor.Web.Configuration.ImageProcessorConfiguration.Instance;

foreach (var service in ipc.ImageServices)
{
foreach (var key in service.Settings.Keys) {
<tr>
<td>@service.GetType().ToString().@key</td>
<td>@service.Settings[key]</td>
</tr>
}
}

foreach (var processors in ipc.AvailableWebGraphicsProcessors)
{
foreach (var key in processors.Value.Keys)
{
<tr>
<td>@processors.Key.@key</td>
<td>@processors.Value[key]</td>
</tr>
}

}

foreach (var cacheSettingKey in ipc.ImageCacheSettings.Keys)
{
<tr>
<td>@cacheSettingKey</td>
<td>@ipc.ImageCacheSettings[cacheSettingKey]</td>
</tr>

}
}
</table>
</div>
</div>
</body>
Expand Down
5 changes: 5 additions & 0 deletions tests/ImageProcessor.TestWebsite/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!-- Configure settings using the format ImageProcessor.<CacheName|PluginName|ServiceName>.SettingKey, you would use this option to customise settings in different deployment slots -->
<!-- The setting must exist in the relevant .config file before the settings below will be applied -->
<add key="ImageProcessor.RemoteImageService.Timeout" value="29000"/>
<add key="ImageProcessor.GaussianBlur.MaxSize" value="20"/>
<add key="ImageProcessor.DiskCache.VirtualCachePath" value="~/App_Data/cache1"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
Expand Down

0 comments on commit 8dc54a4

Please sign in to comment.