smithsson68@gmail.com
Jun 14, 2010
  5801
(0 votes)

Updating configuration from code in CMS 6 onwards

With the introduction of the EPiServer Framework in EPiServer CMS 6, there are potentially multiple places in the EPiServer codebase that can update configuration from code. As the configuration is also often cached in memory, this can cause problems when trying to update it, as the cache may be out of date which will cause an exception to be thrown on saving.

To alleviate this problem a new API was introduced.

EPiServer.Framework.Configuration.GlobalConfigurationManager

This class contains two public members:

public static void Save(System.Configuration.Configuration configuration, RestartMode restartMode)

This allows you to save the configuration and specify what should happen in terms of restarting the web application. RestartMode is defined as follows:

public enum RestartMode
{
    None, // Don't restart the app (rely on default behavior)
    ThisApplicationOnly, // Restart this web only
    AllApplications // Restart all web apps that share same config
}

RestartMode is required because configuration is divided into several files. If the configuration changed is hosted in web.config then the normal ASP.NET behavior defined will occur regardless of what RestartMode value is passed. This is also the case if the configuration changed is hosted in another file but it's section handler in web.config has the restartOnExternalChanges attribute set to true.

public static event EventHandler ConfigurationSaved;

This event should be subscribed to by code that holds a cached version of the configuration. When the event is fired, your code should re-read the configuration from disk.

This API applies to all EPiServer products that ship with the EPiServer Framework.

Jun 14, 2010

Comments

Sep 21, 2010 10:33 AM

Nice post Paul - but I still 'really really' dislike EPiServer's functionality that allows it to update its own configuration files. It makes configuration management / source control and deployments unnecessarily complicated.

I would really love to see any EpiServer UI which allows an update to a configuration element completely 'obliterated' :)

IMHO, any configuration change that requires an application restart should be the responsibility of a system administrator or developer (working on the server itself) not the application itself. If the change doesn't require an application restart then why store it as a configuration element - persisting these values in the database would make more much sense and really help with configuration versioning.


Please login to comment.
Latest blogs
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024