zoukankan      html  css  js  c++  java
  • .NET中如何自定义配置节点

      .NET Framework在web.config或app.config中默认提供了很多种设置,以便能够改变应用程序内嵌组件的行为,例如<connectionStrings>、<httpHandlers>、<sessionState>等等,这对于常规情况下的一般程序员而言是足够用的,也是非常方便的。但是我们越来越多地发现需要自己来控制一系列设置的集合 - 有时是面向组件的(自定义的或第三方提供的),有时是应用程序中使用的一系列值的集合。

      .config虽然默认提供了自定义设置(在<appSettings/>节点下),但是它太弱了,仅仅只支持键值对(key/value)

    <add key="myKey" value="myValue"/>

       虽然键值对在大多数情况下也是很有帮助的,但是它对于健壮的组件、复杂的设置而言是不够灵活的,甚至显得过于简单。幸运的是,微软为我们提供了一种以编程的方式来访问自定义的配置/设置。

    一、The Configuration Section

       先看如下的配置设置,含义很明确:定义一些rss,有名字、地址、以及是否缓存

      <feedRetriever>
        <feeds>
          <add name="cnblogs" url="http://www.cnblogs.com/rss/" />
          <add name="CoolShell" url="http://coolshell.cn/rss/" cache="false" />
        </feeds>
      </feedRetriever>

      接下来我们就以代码来展示如何调用 

    二、Writing the Configuration Handler

    1. Representing the <add/> Element

      每一个ConfigurationElement对象都是作为其内部属性(properties)集合的索引器而存在,再通过.NET提供了另一个属性(Attribute)-- 从而使得<add/>(.config中的自定义节点,取名add完全是基于微软是如此命名故为保持一致而为之)节点的attributesFeedElement的properties进行关联(map),具体代码如下 

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace WebAppCustomConfiguration
    {
        public class FeedElement : ConfigurationElement
        {
            [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
            public string Name
            {
                get { return (string)this["name"]; }
                set { }
            }
    
            [ConfigurationProperty("url", IsRequired = true, DefaultValue = "http://localhost")]
            [RegexStringValidator(@"http?://S+")]
            public string Url
            {
                get { return (string)this["url"]; }
                set { }
            }
    
            [ConfigurationProperty("cache", IsRequired = false, DefaultValue = true)]
            public bool Cache
            {
                get { return (bool)this["cache"]; }
                set { }
            }
        }
    }

     The following list is a complete list of possible parameters:

    • DefaultValue – Gets or sets the default value for the decorated property. This parameter is not required.
    • IsDefaultCollection – Gets or a sets a Boolean value indicating whether the property is the default property collection for the decorated property. This parameter is not required, and the default is false.
    • IsKey – Gets or sets a Boolean value indicating whether this property is a key property for the decorated element property. This parameter is not required, and its default value is false.
    • IsRequired – Gets or sets a Boolean value indicating whether the decorated element property is required. This parameter is not required, and its default value is false.

    2. Writing an Element Collection Class

      类ConfigurationElementCollection包含有好几个成员,但是只有两个标记为abstract,所以最简单的ConfigurationElementCollection只需实现这两个方法

    • CreateNewElement() – 创建一个新的ConfigurationElement对象(本例而言就是FeedElement);
    • GetElementKey() – 获得指定配置节点的key (本例而言就是FeedElement的Name属性(property). 
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace WebAppCustomConfiguration
    {
        [ConfigurationCollection(typeof(FeedElement))]
        public class FeedElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new FeedElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((FeedElement)element).Name;
            }
        }
    }

    3. Writing the FeedRetreiverSection Class

      此Class较为简单,只需编程实现能够访问<feeds />节点即可,具体而言就是一属性(Property)

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    
    namespace WebAppCustomConfiguration
    {
        public class FeedRetrieverSection : ConfigurationSection
        {
            [ConfigurationProperty("feeds", IsDefaultCollection = true)]
            public FeedElementCollection Feeds
            {
                get { return (FeedElementCollection)this["feeds"]; }
                set { }
            }
        }
    }

    4. Modifying web.config

      下面的配置就不说了

    <section name="feedRetriever" type="WebAppCustomConfiguration.FeedRetrieverSection"/>

    5. Accessing Configuration Data from Code

      调用的代码如下,简单不解释

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    
    namespace WebAppCustomConfiguration
    {
        public class FeedRetriever
        {
            public static FeedRetrieverSection _Config = ConfigurationManager.GetSection("feedRetriever") as FeedRetrieverSection;
    
            public static void GetFeeds()
            {
                foreach (FeedElement feedElement in _Config.Feeds)
                {
                    // make request  
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(feedElement.Url);
                    WebProxy proxy = new WebProxy("ip address:port", true);
                    proxy.Credentials = new NetworkCredential("username", "password", "domain");
                    request.Proxy = proxy;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string feedData = String.Empty;
    
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            feedData = reader.ReadToEnd();
                        }
    
                        if (feedElement.Cache)
                        {
                            // filename of cache file  
                            string filename = String.Format("{0}_{1}.xml", feedElement.Name, DateTime.Now.Ticks);
    
                            // cache file  
                            using (StreamWriter writer = new StreamWriter(@"D:DeploymentResult" + filename))
                            {
                                writer.Write(feedData);
                            }
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    Android6.0以后动态增加权限
    Failed to resolve: junit:junit:4.12
    tflite
    error: undefined reference to `cv::imread(std::string const&, int)'
    Makefile
    tf模型可视化工具
    linux c++下遍历文件
    mobilenetV3
    centos7安装mxnet
    chrome的一些插件
  • 原文地址:https://www.cnblogs.com/panchunting/p/ASPNET_how-to-add-custom-configuration-settings-for-your-asp-net-application.html
Copyright © 2011-2022 走看看