背景

对于编译型应用程序来说,参数化程序行为是非常有必要的,.NET有其标准的配置方法,我们可以可以扩展。

示例

代码

 using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml; namespace YEA.Infrastructure.Gateway.Config
{
public enum ExchangeType { direct, topic, fanout };
public class Exchange : ConfigurationElement
{
[ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
public string Name
{
get { return (string) this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("connection", DefaultValue = "", IsRequired = true)]
public string Connection
{
get { return (string)this["connection"]; }
set { this["connection"] = value; }
}
[ConfigurationProperty("type", DefaultValue = "direct", IsRequired = true)]
public ExchangeType Type
{
get { return (ExchangeType)this["type"]; }
set { this["type"] = value; }
}
[ConfigurationProperty("autodelete", DefaultValue= true, IsRequired = false)]
public bool AutoDelete
{
get { return (bool)this["autodelete"]; }
set { this["autodelete"] = value; }
}
[ConfigurationProperty("durable", DefaultValue = false, IsRequired = false)]
public bool Durable
{
get { return (bool)this["durable"]; }
set { this["durable"] = value; }
}
}
public class ExchangeCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Exchange();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Exchange)element).Name;
}
public new string AddElementName
{
get { return base.AddElementName; }
set { base.AddElementName = value; }
}
public new string ClearElementName
{
get { return base.ClearElementName; }
set { base.ClearElementName = value; } } public new string RemoveElementName
{
get { return base.RemoveElementName; }
} public new int Count
{
get { return base.Count; }
} public Exchange this[int index]
{
get
{
return (Exchange)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
} new public Exchange this[string Name]
{
get { return (Exchange)BaseGet(Name); }
} public int IndexOf(Exchange exchange)
{
return BaseIndexOf(exchange);
} public void Add(Exchange exchange)
{
BaseAdd(exchange);
// Add custom code here.
} protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
// Add custom code here.
} public void Remove(Exchange exchange)
{
if (BaseIndexOf(exchange) >= )
BaseRemove(exchange.Name);
} public void RemoveAt(int index)
{
BaseRemoveAt(index);
} public void Remove(string name)
{
BaseRemove(name);
} public void Clear()
{
BaseClear();
// Add custom code here.
}
}
public class AMQPObjectsDeclarationSection : ConfigurationSection
{
[ConfigurationProperty("ExchangeList", IsDefaultCollection = false)]
public ExchangeCollection ExchangeList
{
get
{
ExchangeCollection exchanges = (ExchangeCollection)this["ExchangeList"];
return exchanges;
}
}
[ConfigurationProperty("QueueList", IsDefaultCollection = false)]
public QueueCollection QueueList
{
get
{
QueueCollection queue = (QueueCollection)this["QueueList"];
return queue;
}
}
[ConfigurationProperty("BindingList", IsDefaultCollection = false)]
public BindingCollection BindingList
{
get
{
BindingCollection binding = (BindingCollection)this["BindingList"];
return binding;
}
} protected override void DeserializeSection(System.Xml.XmlReader reader)
{
base.DeserializeSection(reader);
// You can add custom processing code here.
} protected override string SerializeSection(
ConfigurationElement parentElement,
string name, ConfigurationSaveMode saveMode)
{
string s =
base.SerializeSection(parentElement,
name, saveMode);
// You can add custom processing code here.
return s;
} }
}

最终XML配置

App.config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="AMQPConnection">
<section name="ConnectionSettings"
type="YEA.Infrastructure.Gateway.Config.ConnectionSection, YEA.Infrastructure.Gateway"></section>
</sectionGroup>
<sectionGroup name="AMQPAdmin">
<section name="AMQPObjectsDeclaration"
type="YEA.Infrastructure.Gateway.Config.AMQPObjectsDeclarationSection, YEA.Infrastructure.Gateway"
allowLocation="true"
allowDefinition="Everywhere"></section>
</sectionGroup> </configSections>
<AMQPConnection >
<ConnectionSettings configSource="Config\amqp.connection.config"/>
</AMQPConnection>
<AMQPAdmin>
<AMQPObjectsDeclaration configSource="Config\amqp.objects.config"/>
</AMQPAdmin>
<appSettings >
<add key="SleepTime" value="0"></add>
<!--Mode = ["Receiver","Transmitter"]-->
<add key="Mode" value="Receiver"></add>
<add key="DummyFile" value="C:\logs\YEA\Monitor\LogService.txt"></add>
<add key="notificationToEmail" value="ruben.rotteveel@chamberlain.com"/>
<add key="notificationFromEmail" value="webmaster@chamberlain.com"/>
</appSettings>
</configuration>

amqp.connection.config

 <?xml version="1.0" encoding="utf-8" ?>
<ConnectionSettings>
<ConnectionList>
<add name="Connection" server="localhost" username="guest" password="guest"></add>
</ConnectionList>
<PublisherList>
<add name="orderPublisher" connection="Connection" exchange="orders" ></add>
<add name="notificationPublisher" connection="Connection" exchange="notificationExchange"></add>
<add name="logPublisher" connection="Connection" exchange="logExchange"></add>
</PublisherList>
<AsyncReceiverList>
<add name="orderReceiver" connection="Connection" queue="uk_orders, marketingemails, Accounting, CustomerService" maxthreads="40"></add>
<add name="notificationReceiver" connection="Connection" queue="notificationQueue" maxthreads ="1"></add>
<add name="logReceiver" connection="Connection" queue="logQueue" maxthreads ="1"></add>
</AsyncReceiverList>
</ConnectionSettings>

amqp.objects.config

 <?xml version="1.0" encoding="utf-8" ?>
<AMQPObjectsDeclaration>
<ExchangeList>
<add name="orders" connection="Connection" type="topic" autodelete="false" durable="true"></add>
<add name="shipments" connection="Connection" type ="topic" autodelete="false" durable="true"></add>
<add name="logExchange" connection="Connection" type="topic" autodelete="false" durable="true"></add>
<add name="notificationExchange" connection="Connection" type="topic" autodelete="false" durable="true"></add>
</ExchangeList>
<QueueList>
<add name="uk_orders" connection="Connection" autodelete="false" durable="true"></add>
<add name="us_orders" connection="Connection" autodelete="false" durable="true"></add>
<add name="marketingemails" connection="Connection" autodelete ="false" durable="true"></add>
<add name="Accounting" connection="Connection" autodelete="false" durable="true" ></add>
<add name="CustomerService" connection="Connection" autodelete="false" durable="true" ></add>
<add name="logQueue" connection="Connection" autodelete="false" durable="true"></add>
<add name="notificationQueue" connection="Connection" autodelete="false" durable="true"></add>
</QueueList>
<BindingList>
<add queue="uk_orders" connection="Connection" exchange="orders" subscriptionkey="order.uk.#"></add>
<add queue="us_orders" connection="Connection" exchange="orders" subscriptionkey="order.us.#"></add>
<add queue="marketingemails" connection="Connection" exchange="shipments" subscriptionkey="#" ></add>
<add queue="marketingemails" connection="Connection" exchange="orders" subscriptionkey="#" ></add>
<add queue="Accounting" connection="Connection" exchange="shipments" subscriptionkey="#" ></add>
<add queue="Accounting" connection="Connection" exchange="orders" subscriptionkey="#" ></add>
<add queue="CustomerService" connection="Connection" exchange="shipments" subscriptionkey="#" ></add>
<add queue="CustomerService" connection="Connection" exchange="orders" subscriptionkey="#" ></add>
<add queue="logQueue" connection="Connection" exchange="logExchange" subscriptionkey="#"></add>
<add queue="notificationQueue" connection="Connection" exchange="notificationExchange" subscriptionkey="#"></add>
</BindingList>
</AMQPObjectsDeclaration>

读取配置

             var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var objects = config.GetSection("AMQPAdmin/AMQPObjectsDeclaration") as AMQPObjectsDeclarationSection;

备注

简单的使用配置并不是最好的方式,比如:在控制台程序中,最好提供两种方式:通过命令行参数和配置结合,然后采用适当的优先级处理。另外,采用JSON配置可能会更简单一点。

.NET:自定义配置节的更多相关文章

  1. C#创建自定义配置节

    在.Net应用程序中,我们经常看到VS为我们生成的项目工程中都会含有nfig或者nfig这样的文件.这个文件就是我们所说的应用程序配置文件.在这个文件里面记述着一些与我们的应用程序相关的信息,如:数据 ...

  2. C#如何使用和开发自定义配置节

    在日常的程序设计中,如何灵活和巧妙地运用配置信息是一个成功的设计师的首要选择.这不仅是为了程序设计得更灵活性和可扩展性,也是为了让你的代码给人以清新的感觉.程序中的配置信息一般放在应用程序的app.c ...

  3. 使用 ConfigurationSection 创建自定义配置节

    我们可以通过用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集,要完成这一功能,我们必须实现继承System.Configuration.ConfigurationSection 类来 ...

  4. C# App.config 自定义 配置节

    1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration>  ...

  5. C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法

    App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...

  6. C# App.config 自定义 配置节 出现的问题:配置系统未能初始化

    C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...

  7. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合

    核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...

  8. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节

    主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...

  9. App.config 中读写appSettings、system.serviceModel终结点,以及自定义配置节

    转自:http://blog.csdn.net/chelen_jak/article/details/8190795 感觉写的很好,推荐

随机推荐

  1. 使用android模拟器开发程序

    自从android studio升级到3.0之后自带的模拟器已经很好用了,尤其是升级后可以想vmware那样休眠,再次开启就可以快速启动了 以下是几点可以更方便地使用系统模拟器进行开发的小技巧,毕竟模 ...

  2. ldconfig命令与ldd命令

    ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令 ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令. ...

  3. linux后端诊断与调试技术

    本文不是liunx命令使用教程,也不打算全方面阐明其用法,互联网公司项目很多,服务程序之间相互依赖调用很复杂,各种因素会影响线程服务正常运行,特别是基础服务组件更是如此,当出现各种问题时,如何诊断li ...

  4. (五)消费Dubbo服务

    前面我们搞了发布Dubbo服务,发布的服务就是用来消费的,所以我们这里来调用服务,消费下: 创建maven项目 dubbo-demo-consumer pom.xml配置下: <dependen ...

  5. html圈圈

    <html> <head> <meta charset="utf-8" /> <link href="images/style. ...

  6. hdu 5443 (2015长春网赛G题 求区间最值)

    求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...

  7. 【LOJ】#2568. 「APIO2016」烟花表演

    题解 这个听起来很毒瘤的想法写起来却非常休闲,理解起来可能很费劲 例如,我们首先到猜到答案是个下凸包 然后是不是要三分???然而并不是orz 我们通过归纳证明这个下凸包的结论来总结出了一个算法 也就是 ...

  8. 【LOJ】#2492. 「BJOI2018」二进制

    题解 每次开这样的数据结构题感想都大概是如下两点 1.为什么别人代码长度都是我的1/2???? 2.为什么我运行时间都是他们的两倍???? 简单分析一下,我们关注一个区间是否合法只关注这个区间有多少个 ...

  9. HDU 6249 Alice’s Stamps

    [题目链接] 题目大意: 说有$m$个区间,要求选出不超过$k$个区间,使这些区间覆盖的长度最长,问最长长度是多少. 题解: 所有区间按$R$从小到大排序之后可以进行$dp$. $dp[i][j]$表 ...

  10. 【Vue实战之路】一、Vue-cli入门及Vue工程目录全解。

    全面的Vue-cli学习,这一篇就够了! 一.下载 使用vue-cli前,需先安装node.js,node的安装就不赘述,不过在此需要注意: 1. node版本需在4.x以上,首推6.x以上版本(no ...