<configuration>
<configSections>
<!--1.自定义一个节 CustomSection -->
<section name="CustomSection"
type="CustomSection.BatchingHostingSettings,
CustomSection"/>
<!--2.为CustomSection 这个节配置子节点-->
<CustomSection>
<add type="CustomSection.FooService, CustomSection"/>
<add type="CustomSection.BarService, CustomSection"/>
<add type="CustomSection.BazService, CustomSection"/>
</CustomSection>
</configuration>
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<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();
}
}
}
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();
}