如何定义与潜在的子元素定制的web.config型材和性能属性? [英] How do I define custom web.config sections with potential child elements and attributes for the properties?

查看:121
本文介绍了如何定义与潜在的子元素定制的web.config型材和性能属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web应用程序,我经常需要发展相互依存的配置设置,也有认为必须改变,因为我们每一个我们的环境之间移动设置。

The web applications I develop often require co-dependant configuration settings and there are also settings that have to change as we move between each of our environments.

我们所有的设置都简单当前的键值对,但是这将是有益创建自定义配置节,这样很明显当两个值需要改变在一起,或者当设置需要改变的环境。

All our settings are currently simple key value pairs but it would be useful to create custom config sections so that it is obvious when two values need to change together or when the settings need to change for an environment.

什么是最好的方式来创建自定义配置部分以及是否有什么特殊的考虑检索值时,做什么呢?

What's the best way to create custom config sections and are there any special considerations to make when retrieving the values?

推荐答案

使用属性,子配置部分和约束

有也是使用属性自动取水管的护理,以及提供轻松地添加约束的能力的可能性。

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.

我在这里present从code我用我自己在我的网站的一个例子。有了约束我口述的磁盘空间,任何一个用户被允许使用的最高金额。

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.

MailCenterConfiguration.cs:

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

这是建立在web.config中像这样

This is set up in web.config like so

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

子元素

子XML元素的邮件的创建在同一个cs文件上面那样的创建。在这里,我在该端口上添加的约束。如果端口在此范围内分配一个值不运行时会抱怨当配置被加载。

The child xml element mail is created 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.

MailCenterConfiguration.cs:

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

使用

要然后使用它实际上在code,你所要做的就是实例化MailCenterConfigurationObject,这将自动从web.config中读取相关章节。

To then use it practically in code, all you have to do is instantiate the MailCenterConfigurationObject, this will automatically read the relevant sections from web.config.

MailCenterConfiguration.cs

MailCenterConfiguration.cs

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

        return instance;
    }
}

AnotherFile.cs

AnotherFile.cs

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

检查的有效性

我previously(以MailCenterConfiguration.cs例如)提到,当配置加载一些数据不符合你已经设置了规则,运行时会报错。我往往想尽快知道这些事情时,我的网站会调用。解决这个的一种方式是装载在_Global.asax.cx.Application_Start_的结构中,如果配置是无效的,你将与异常的装置被通知这一点。您的网站将不会启动,而是你将是presented中的黄色屏幕的死亡的。

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 Yellow screen of death.

的Global.asax.cs

Global.asax.cs

protected void Application_ Start(object sender, EventArgs e)
{
    MailCenterConfiguration.Instance;
}

这篇关于如何定义与潜在的子元素定制的web.config型材和性能属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆