背景

对于编译型应用程序来说,参数化程序行为是非常有必要的,.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. IDEA中Ctrl+Shift+F快捷键无效的解决方式

    某天突然发现idea非常重要的快捷键ctrl+shift+F无效了,网上搜了很多都说是qq快捷键冲突,但是找了下qq快捷键却没有解决,现在给大家一个解决快捷键冲突的思路: 1.查看QQ快捷键--> ...

  2. NET 架构指南频道

    NET 架构指南频道 微软在Visual Studio 2017 正式发布的时候也上线了一个参考应用https://github.com/dotnet/eShopOnContainers , 最近微软 ...

  3. 关于 facebook

    2017/10/29 Facebook账号分分钟被禁用,见怪不怪就好了,禁了就申诉呗 Facebook 如果遇到帐号被停用 / 帐号被封锁,大致上来说有叁个原因: 1, 名字用假名 2, 一个人拥有多 ...

  4. mongo连接数满问题处理

    记一次mongo服务端无法建立更多连接造成的客户端无法访问mongo集群的故障分析及解决 一. 问题: 程序无法连接mongo集群 现象: 2017-09-05T01:29:08.765+0000 I ...

  5. Lighthouse前端性能优化测试工具

    在前端开发中,对于自己开发的app或者web page性能的好坏,一直是让前端开发很在意的话题.我们需要专业的网站测试工具,让我们知道自己的网页还有哪些需要更为优化的方面,我自己尝试了一款工具:Lig ...

  6. SpringBoot详细研究-02数据访问

    Springboot对数据访问部分提供了非常强大的集成,支持mysql,oracle等传统数据库的同时,也支持Redis,MongoDB等非关系型数据库,极大的简化了DAO的代码,尤其是Spring ...

  7. Ubuntu 编译安装 nDPI

    1.安装gcc 和 build-essential sudo apt-get install gccsudo apt-get install build-essential 2.安装必要的软件 sud ...

  8. python3 开发面试题(collections中的Counter)6.7

    ''' 编写Python脚本,分析xx.log文件,按域名统计访问次数 xx.log文件内容如下: https://www.sogo.com/ale.html https://www.qq.com/3 ...

  9. ThreadLocal 详解

    什么是ThreadLocal 根据JDK文档中的解释:ThreadLocal的作用是提供线程内的局部变量,这种变量在多线程环境下访问时能够保证各个线程里变量的独立性. 从这里可以看出,引入Thread ...

  10. ant design的一些坑

    1.在本地修改ant design的某些样式可以生效,但在线上就失效了.比如collapse组件里的箭头图标在本地和在线上的类名有变化,本地类名,线上类名:箭头图标的svg样式在线上会自动添加一个内联 ...