DevelopmentDynamic 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.

 

https://aeriontech.wpenginepowered.com/wp-content/uploads/2021/03/Aerion-Logo-Vector-1_583-1.png
Connect with us

Subscribe to our newsletter today to receive updates on the latest news, releases and special offers. We respect your privacy. Your information is safe.

©2023 Aerion Technologies. All rights reserved | Terms of Service | Privacy Policy