Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Using attributes, child config sections and constraints</strong></p> <p>There is also the possibility to use attributes which automatically takes care of the plumbing, as well as providing the ability to easily add constraints.</p> <p>I here present an example from code I use myself in one of my sites. With a constraint I dictate the maximum amount of disk space any one user is allowed to use.</p> <p>MailCenterConfiguration.cs:</p> <pre><code>namespace Ani { public sealed class MailCenterConfiguration : ConfigurationSection { [ConfigurationProperty("userDiskSpace", IsRequired = true)] [IntegerValidator(MinValue = 0, MaxValue = 1000000)] public int UserDiskSpace { get { return (int)base["userDiskSpace"]; } set { base["userDiskSpace"] = value; } } } } </code></pre> <p>This is set up in web.config like so</p> <pre><code>&lt;configSections&gt; &lt;!-- Mailcenter configuration file --&gt; &lt;section name="mailCenter" type="Ani.MailCenterConfiguration" requirePermission="false"/&gt; &lt;/configSections&gt; ... &lt;mailCenter userDiskSpace="25000"&gt; &lt;mail host="my.hostname.com" port="366" /&gt; &lt;/mailCenter&gt; </code></pre> <p><strong>Child elements</strong></p> <p>The child xml element <em>mail</em> is created in the same .cs file as the one above. Here I've added constraints on the port. If the port is assigned a value not in this range the runtime will complain when the config is loaded.</p> <p>MailCenterConfiguration.cs:</p> <pre><code>public sealed class MailCenterConfiguration : ConfigurationSection { [ConfigurationProperty("mail", IsRequired=true)] public MailElement Mail { get { return (MailElement)base["mail"]; } set { base["mail"] = value; } } public class MailElement : ConfigurationElement { [ConfigurationProperty("host", IsRequired = true)] public string Host { get { return (string)base["host"]; } set { base["host"] = value; } } [ConfigurationProperty("port", IsRequired = true)] [IntegerValidator(MinValue = 0, MaxValue = 65535)] public int Port { get { return (int)base["port"]; } set { base["port"] = value; } } </code></pre> <p><strong>Use</strong></p> <p>To then use it practically in code, all you have to do is instantiate the MailCenterConfigurationObject, this will <strong>automatically</strong> read the relevant sections from web.config.</p> <p>MailCenterConfiguration.cs</p> <pre><code>private static MailCenterConfiguration instance = null; public static MailCenterConfiguration Instance { get { if (instance == null) { instance = (MailCenterConfiguration)WebConfigurationManager.GetSection("mailCenter"); } return instance; } } </code></pre> <p>AnotherFile.cs</p> <pre><code>public void SendMail() { MailCenterConfiguration conf = MailCenterConfiguration.Instance; SmtpClient smtpClient = new SmtpClient(conf.Mail.Host, conf.Mail.Port); } </code></pre> <p><strong>Check for validity</strong></p> <p>I previously mentioned that the runtime will complain when the configuration is loaded and some data does not comply to the rules you have set up (e.g. in MailCenterConfiguration.cs). I tend to want to know these things as soon as possible when my site fires up. One way to solve this is load the configuration in _Global.asax.cx.Application_Start_ , if the configuration is invalid you will be notified of this with the means of an exception. Your site won't start and instead you will be presented detailed exception information in the <a href="http://www.codinghorror.com/blog/images/yellow-screen-of-death-large.png" rel="noreferrer">Yellow screen of death</a>.</p> <p>Global.asax.cs</p> <pre><code>protected void Application_ Start(object sender, EventArgs e) { MailCenterConfiguration.Instance; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload