Dynamic Configuration of ImageProcessor .Net with Azure App Service Slot Settings
At Aerion we use the awesome ImageProcessor library (A .NET library for on-the-fly processing of images) when we need to have image caching and the ability to request images at different sizes based on designs. As with most things when moving the code to an Azure App Service we try to utilise the Slot Settings so that we can have different configurations for the various slots that we use.
This posed a slight problem with the ImageProcessor library and the Azure cache plug-in, as there didn’t seem to be a documented way to dynamically configure the plugin and change Storage connection strings without recompilation. We started digging through the issues within the GitHub project and found a proposed solution that had been merged into the code base. https://github.com/JimBobSquarePants/ImageProcessor/issues/432
To allow dynamic configuration based on Slot Settings we need to extend the existing Azure Cache module
namespace #######.ImageProcessServices
{
public class AzureBlobCacheFromAppSettings : AzureBlobCache
{
public AzureBlobCacheFromAppSettings(string requestPath, string fullPath, string querystring)
: base(requestPath, fullPath, querystring)
{
}
protected override void AugmentSettings(Dictionary<string, string> settings)
{
settings["CachedStorageAccount"] = ConfigurationManager.AppSettings["CachedStorageAccount"]?.ToString();
settings["CachedBlobContainer"] = ConfigurationManager.AppSettings["CachedBlobContainer"]?.ToString();
}
}
}
The key part to the above code is the AugmentSettings override. The base constructor for ImageCacheBase calls AugmentSettingsCore which in turn call AugmentSettings. This allows us an entry point to modify the settings for the plugin before the rest of it is initialised. By setting the storageAccount and Container, we allow the plugin to use the connection string provided by the app settings and also specify the cache folder.
Now you are able to use a different storage account or different cache folder based on the different slots you have set up.
The above code example could be improved with some error handling or by providing default values if the keys are not present.
Related
Originally published at dev.aerion.com.au on October 19, 2018.