January 13, 2006

Custom Configuration Handlers In .Net

Implementing 'ConfigurationSectionHandler' objects. Think I may have an error in this code but nothing to difficult to resolve In the '.config' file for the application 1. Specify the handler within the 'configsections' area 2. Add the section as a child node of the 'configuration' node
  <section name="mysection"
type="mynamespace.mysection.StuffConfigurationSectionHandler, Stuff" /> </configSections> ... <mysection TransportStreamPacketSize="188"> <download MaxMessageRetries="5" MaxWaitPeriod="5000"/> <upload MaxMessageRetries="5" MaxWaitPeriod="5000"> </upload> </mysection> ... </configuration>
Then write a 'ConfigurationSectionHandler' class for it This handler must be invoked somewhere for it to do something
using System.Configuration;

// Handles the DescriptorFactory configuration
section of the application configuration file. public class StuffConfigurationSectionHandler
: IConfigurationSectionHandler { #region Logging //static readonly Logging.ILog log = ... #endregion Logging #region Private Data private static object threadLock = new object(); private static bool configurationComplete = false; #endregion Private Data #region IConfigurationSectionHandler Members // This method is called as a result of a call to // ConfigurationSettings.GetConfig(). // It sets the configuration data in // the PsiSiComms configuration object public object Create(object parent,
object configContext, XmlNode section) { lock(threadLock) { if (!configurationComplete) { log.Info("Configuring XXX."); RetrieveConfiguration(section); configurationComplete = true; log.Info("PsiSiComms configuration complete."); } } return null; } #region Private Helper Methods private void RetrieveConfiguration(XmlNode parent) { XmlNode node; XmlNode uploadNode = parent.SelectSingleNode("./upload"); if (!ReferenceEquals(uploadNode, null)) { node = uploadNode.Attributes.GetNamedItem
("MaxMessageRetries"); if (!ReferenceEquals(node, null)) { MyXXXConfig.Upload.MaxMessageRetries
= int.Parse(node.Value); } node = uploadNode.Attributes.GetNamedItem
("MaxWaitPeriod"); if (!ReferenceEquals(node, null)) { MyXXXConfig.Upload.MaxWaitPeriod
= int.Parse(node.Value); } RetrieveUploadTablesConfiguration(uploadNode); } } #endregion Private Helper Methods #endregion }

No comments: