<configuration>
<configSections>
<!--.自定义一个节 CustomSection -->
<section name="CustomSection"
type="CustomSection.BatchingHostingSettings,
CustomSection"/>
<!--.为CustomSection 这个节配置子节点-->
<CustomSection>
<add type="CustomSection.FooService, CustomSection"/>
<add type="CustomSection.BarService, CustomSection"/>
<add type="CustomSection.BazService, CustomSection"/>
</CustomSection>
</configuration>
.定义转换器
public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string typeName = (string)value;
if (string.IsNullOrEmpty(typeName))
{
return null;
}
Type result = Type.GetType(typeName, false);
if (result == null)
{
throw new ArgumentException(string.Format("不能加载类型\"{0}\"", typeName));
}
return result;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
Type type = value as Type;
if (null == type)
{
throw new ArgumentNullException("value");
}
return type.AssemblyQualifiedName;
}
}
.配置元素集合的元素类型的配置
public class ServiceTypeElement : ConfigurationElement
{
[ConfigurationProperty("type", IsRequired = true)]
[TypeConverter(typeof(AssemblyQualifiedTypeNameConverter))]
public Type ServiceType
{
get { return (Type)this["type"]; }
set { this["type"] = value; }
}
}
.定义配置元素集合
public class ServiceTypeElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ServiceTypeElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
ServiceTypeElement serviceTypeElement = (ServiceTypeElement)element;
return serviceTypeElement.ServiceType.MetadataToken;
}
}
.自定义节对应的类的定义
public class BatchingHostingSettings : ConfigurationSection
{
[ConfigurationProperty("", IsDefaultCollection = true)]
public ServiceTypeElementCollection ServiceTypes
{
get { return (ServiceTypeElementCollection)this[""]; }
} public static BatchingHostingSettings GetSection()
{
return ConfigurationManager.GetSection("CustomSection")
as BatchingHostingSettings;
}
}
. 对应要解析的类
public class FooService
{
}
public class BarService
{
}
public class BazService
{
}
.ServiceHostCollection
public class ServiceHostCollection : Collection<ServiceHost>, IDisposable
{ public ServiceHostCollection(params Type[] serviceTypes)
{ BatchingHostingSettings settings = BatchingHostingSettings.GetSection();
foreach (ServiceTypeElement element in settings.ServiceTypes)
{
this.Add(element.ServiceType);
} if (null != serviceTypes)
{
Array.ForEach<Type>(serviceTypes, serviceType => this.Add(new ServiceHost(serviceType)));
}
}
public void Add(params Type[] serviceTypes)
{
if (null != serviceTypes)
{
Array.ForEach<Type>(serviceTypes, serviceType => this.Add(new ServiceHost(serviceType)));
}
}
public void Open()
{
foreach (ServiceHost host in this)
{
host.Open();
}
}
public void Dispose()
{
foreach (IDisposable host in this)
{
host.Dispose();
}
}
}
.演示一个服务寄宿的例子
protected void Page_Load(object sender, EventArgs e)
{
ServiceHostCollection list = new ServiceHostCollection();
foreach (ServiceHost host in list)
{
host.Opened += (sender1, arg1) =>
{
System.Diagnostics.Debug.WriteLine("服务{0}开始监听",
(sender1 as ServiceHost).Description.ServiceType);
};
}
list.Open();
Console.Read(); }

最新文章

  1. String对象方法扩展
  2. 关于redis的主从复制
  3. Ubuntu android 开发配置
  4. jQuery源代码学习之六——jQuery数据缓存Data
  5. VC++ operate excel
  6. Mellanox OFED2.1-X安装记录
  7. JLayer初体验。。
  8. SpringMVC整合极光推送报错ClassNotFound
  9. java web方面的面试问题,Spring MVC方面的面试问题,摘自java web轻量级开发面试教程
  10. JVM学习八:常用JVM配置参数
  11. Codeforces 558E A Simple Task (计数排序&amp;&amp;线段树优化)
  12. react-devtool 消息处理渲染 源码理解
  13. Java并发包1--线程的状态及常用方法
  14. Delphi动态添加菜单
  15. BZOJ4377[POI2015]Kurs szybkiego czytania——数学思维题
  16. A very simple C++ module to encrypt/decrypt strings based on B64 and Vigenere ciper.
  17. awk使用教程
  18. [Python]-pip-ReadTimeoutError: Read timed out 问题
  19. 喵哈哈村的魔法考试 Round #2 (Div.2) 题解
  20. SQL2005 安装问题

热门文章

  1. rabbitmq学习(一):AMQP协议,AMQP与rabbitmq的关系
  2. TCP滑动窗口与回退N针协议
  3. 【java多线程】java的线程池分析
  4. Netty5.x 和3.x、4.x的区别及注意事项(官方翻译)
  5. CollabNet Subversion Edge 安装笔记(1):基本安装设定篇
  6. bzoj 4372 烁烁的游戏——动态点分治+树状数组
  7. Linux环境安装jdk10
  8. 使用正则表达式去除html标签
  9. Java renameTo()重新命名此抽象路径名表示的文件
  10. Bootstrap-CL:超大屏幕