博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net 自定义节配置 (configSections下的section)
阅读量:6188 次
发布时间:2019-06-21

本文共 4030 字,大约阅读时间需要 13 分钟。

3.定义转换器 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; } }4.配置元素集合的元素类型的配置 public class ServiceTypeElement : ConfigurationElement { [ConfigurationProperty("type", IsRequired = true)] [TypeConverter(typeof(AssemblyQualifiedTypeNameConverter))] public Type ServiceType { get { return (Type)this["type"]; } set { this["type"] = value; } } }5.定义配置元素集合 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; } }6.自定义节对应的类的定义 public class BatchingHostingSettings : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public ServiceTypeElementCollection ServiceTypes { get { return (ServiceTypeElementCollection)this[""]; } } public static BatchingHostingSettings GetSection() { return ConfigurationManager.GetSection("CustomSection") as BatchingHostingSettings; } }7. 对应要解析的类 public class FooService { } public class BarService { } public class BazService { }8.ServiceHostCollection public class ServiceHostCollection : Collection
, 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
(serviceTypes, serviceType => this.Add(new ServiceHost(serviceType))); } } public void Add(params Type[] serviceTypes) { if (null != serviceTypes) { Array.ForEach
(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(); } } }9.演示一个服务寄宿的例子 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(); }

 

转载于:https://www.cnblogs.com/kexb/p/6603528.html

你可能感兴趣的文章
直播多人连麦技术简介
查看>>
《自动化办公》两秒完成250页豆瓣电影PPT
查看>>
作为前端,你不得不知道的搜索引擎优化
查看>>
编译deno,deno结构解析
查看>>
推动快递保价大众化,顺丰、京东、通达系谁更彻底?
查看>>
Qtum量子链研究院:Qtum Plasma MVP 技术详解
查看>>
VR全景图片浏览实现
查看>>
【译】 WebSocket 协议第九章——扩展(Extension)
查看>>
深入call apply bind
查看>>
22. Generate Parentheses
查看>>
函数运行环境系统动态链接库版本太低?函数计算 fun 神助力分忧解难
查看>>
如何让自己看起来更专业?前端程序员必须了解的几个“词语”
查看>>
网页水印SDK的实现
查看>>
Spring - Configuration Metadata
查看>>
利用Python调用HBASE
查看>>
python socket编程之基本流程
查看>>
mysql字符乱码
查看>>
Mysql之一:mysqldump和LVM逻辑卷快照
查看>>
JAVA之带转义字符的json字符串解析
查看>>
Mac上搭建Xcode9.0+appium1.6.5过程及链接模拟器测试app
查看>>