How do I specify personalized web.config areas with possible youngster components and also features for the buildings?

The internet applications I create usually call for carbon monoxide - dependant arrangement setups and also there are additionally setups that need to transform as we relocate in between each of our settings.

All our setups are presently straightforward key value sets yet it would certainly serve to create personalized config areas to make sure that it is noticeable when 2 values require to transform with each other or when the setups require to transform for a setting.

What's the most effective means to create personalized config areas and also exist any kind of unique factors to consider to make when fetching the values?

0
2019-05-09 11:11:04
Source Share
Answers: 3

Using features, youngster config areas and also restraints

There is additionally the opportunity to make use of features which instantly cares for the pipes, along with giving the capacity to conveniently add restraints.

I below offer an instance from code I utilize myself in among my websites. With a restraint I determine the maximum quantity of disk room any kind of one customer is permitted to make use of.

MailCenterConfiguration.cs:

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; }
        }
    }
}

This is set up in web.config thus

<configSections>
    <!-- Mailcenter configuration file -->
    <section name="mailCenter" type="Ani.MailCenterConfiguration" requirePermission="false"/>
</configSections>
...
<mailCenter userDiskSpace="25000">
    <mail
     host="my.hostname.com"
     port="366" />
</mailCenter>

Child components

The youngster xml component mail is developed in the same.cs submit as the one over. Below I've included restraints on the port. If the port is appointed a value not in this array the runtime will certainly whine when the config is filled.

MailCenterConfiguration.cs:

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; }
        }

Use

To after that utilize it almost in code, all you need to do is instantiate the MailCenterConfigurationObject, this will certainly instantly read the pertinent areas from web.config.

MailCenterConfiguration.cs

private static MailCenterConfiguration instance = null;
public static MailCenterConfiguration Instance
{
    get
    {
        if (instance == null)
        {
            instance = (MailCenterConfiguration)WebConfigurationManager.GetSection("mailCenter");
        }

        return instance;
    }
}

AnotherFile.cs

public void SendMail()
{
    MailCenterConfiguration conf = MailCenterConfiguration.Instance;
    SmtpClient smtpClient = new SmtpClient(conf.Mail.Host, conf.Mail.Port);
}

Check for legitimacy

I formerly stated that the runtime will certainly whine when the configuration is filled and also some information does not abide to the regulations you have actually set up (as an example in MailCenterConfiguration.cs). I often tend to need to know these points asap when my website discharges up. One means to address this is load the configuration in _ Global.asax.cx.Application _ Start _, if the configuration is void you will certainly be alerted of this with the methods of an exemption. Your website will not start and also rather you will certainly exist thorough exemption details in the Yellow screen of death.

Global.asax.cs

protected void Application_ Start(object sender, EventArgs e)
{
    MailCenterConfiguration.Instance;
}
0
2019-05-12 06:49:19
Source

Quick 'n Dirty :

First create your ConfigurationSection and also ConfigurationElement courses :

public class MyStuffSection : ConfigurationSection
{
    ConfigurationProperty _MyStuffElement;

    public MyStuffSection()
    {
        _MyStuffElement = new ConfigurationProperty("MyStuff", typeof(MyStuffElement), null);

        this.Properties.Add(_MyStuffElement);
    }

    public MyStuffElement MyStuff
    {
        get
        {
            return this[_MyStuffElement] as MyStuffElement;
        }
    }
}

public class MyStuffElement : ConfigurationElement
{
    ConfigurationProperty _SomeStuff;

    public MyStuffElement()
    {
        _SomeStuff = new ConfigurationProperty("SomeStuff", typeof(string), "<UNDEFINED>");

        this.Properties.Add(_SomeStuff);
    }

    public string SomeStuff
    {
        get
        {
            return (String)this[_SomeStuff];
        }
    }
}

Then allowed the structure recognize just how to manage your arrangement courses in web.config :

<configuration>
  <configSections>
    <section name="MyStuffSection" type="MyWeb.Configuration.MyStuffSection" />
  </configSections>
  ...

And in fact add your very own area listed below :

  <MyStuffSection>
    <MyStuff SomeStuff="Hey There!" />
  </MyStuffSection>

Then you can utilize it in your code hence :

MyWeb.Configuration.MyStuffSection configSection = ConfigurationManager.GetSection("MyStuffSection") as MyWeb.Configuration.MyStuffSection;

if (configSection != null && configSection.MyStuff != null)
{
    Response.Write(configSection.MyStuff.SomeStuff);
}
0
2019-05-10 20:10:42
Source

You can complete this with Section Handlers. There is a standard review of just how to write one at http://www.codeproject.com/KB/aspnet/ConfigSections.aspx nonetheless it describes app.config which would certainly be virtually the like creating one for usage in web.config. This will certainly permit you to basically have your very own XML tree in the config documents and also do some advanced arrangement.

0
2019-05-10 05:58:28
Source