VS.NET’s out-of-the-box application configuration capabilities are a huge advance on VS6, but they don’t meet the needs of larger distributed systems. Where there are a number of load balanced or clustered production servers, let alone test and development environments, we ideally want a single point of application configuration – securely storing things such as database connection strings that would be a nightmare to manage in each individual web or app config file.
In a previous project I built a set of ‘common utilities’ that were packaged as a COM+ library; each application server had a proxy to it installed. One of these utilities was a class called XConfig (included in the download) which, essentially, loaded an encrypted xml document from an http stream. It was a wondrously useful little class, and one I miss in the framework. So, on a recent distributed .NET project I had one of the developers make something similar. I obviously won’t post that code here, but I will one that I did.
A common question in designing application configuration in large distributed environments is where to configure the configuration? For instance, if we stored the configuration data in a database, where do we store the connection string to it? There is no single answer to this (apart from ‘it depends’), but in many cases I think DNS is a strong candidate. If the configuration is stored in a file (easy to modify), delivered via HTTP (easy to setup, firewall rule friendly, can be load balanced for resilience), then the only configuration required is its address. This is easily managed by a DNS alias that resolves, say, a VIP. The config class can even have the alias hard-coded into it, since the environment into which it is installed will determine the physical location of the file.
The downside? Well, of all the protocols used in most projects, HTTP is perhaps the most latent, so this form of configuration is not going to be lightning fast. The consumers need to make sure they use it wisely – i.e. load it once and cache it – in asp.net pull the configuration on Application_Start; class libraries have several options, one is to use static variables populated from constructors,
as shown in the following example:
private static string _dbConn = null;public Constructor() {
if (_dbConn == null) loadConfig();
}
private void loadConfig() {
const string ERR_CFG_INVALID = "Configuration Not Loaded";
const string ERR_CFG_NOVALUE = "No config value found for: ";
try {
Config cfg = new Config();
_dbConn = cfg["Connection"];
// if the connect string is blank then, reset the static
// variable to null so that loadConfig will be called again
if ( _dbConn.Trim().Length == 0 ) {
_dbConn = null;
throw new Exception(ERR_CFG_NOVALUE) + "Connection";
}
}
catch (Exception ex) {
throw new Exception(ERR_CFG_INVALID, ex);
}
}
If you have a number of such config values you should probably consider making your own internal class for your project that will load and hold them all. For more info see the documentation which is included in the download.
