我试图编写一个具有自定义配置实现的应用程序——为此我写了一个小的虚拟应用程序,以后可以使用它。但是,我只是无法加载配置。关于我在这里缺少的任何想法都会有真正的帮助。
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Configuration section-handler declaration area. -->
<configSections>
<sectionGroup name="SecuritySettingsGroup">
<section name="SecuritySettings" type="ConfigSecHandler.ServicesSection, ConfigSecHandler"/>
</sectionGroup>
<!-- Other <section> and <sectionGroup> elements. -->
</configSections>
<!-- Configuration section settings area. -->
<SecuritySettingsGroup>
<SecuritySettings>
<ServiceSecuritySettings>
<Service svcName="xStore" svcUser="user1" svcPermissions="pemission1"/>
<Service svcName="xStore" svcUser="user2" svcPermissions="pemission3"/>
</ServiceSecuritySettings>
</SecuritySettings>
</SecuritySettingsGroup>
</configuration>
课程
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
namespace ServicesConfigSection {
public class ServicesSection : ConfigurationSection {
[ConfigurationProperty("ServiceSecuritySettings", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(ServiceCollection),
AddItemName = "Service")]
public ServiceCollection Services {
get { return (ServiceCollection)this["ServiceSecuritySettings"]; }
set { this["ServiceSecuritySettings"] = value; }
}
public static ServicesSection GetConfiguration() {
return GetConfiguration("SecuritySettingsGroup/SecuritySettings");
}
public static ServicesSection GetConfiguration(string section) {
return ConfigurationManager.GetSection(section) as ServicesSection;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ServicesConfigSection {
public class ServiceCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new ServiceElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((ServiceElement)element).SvcName;
}
public override ConfigurationElementCollectionType CollectionType {
get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ServicesConfigSection {
public class ServiceElement : ConfigurationElement {
[ConfigurationProperty("svcName", DefaultValue = "", IsRequired = true)]
public String SvcName {
get { return (String)this["svcName"]; }
set { this["svcName"] = value; }
}
[ConfigurationProperty("svcUser", DefaultValue = "", IsRequired = true)]
public String SvcUser {
get { return (String)this["svcUser"]; }
set { this["svcUser"] = value; }
}
[ConfigurationProperty("svcPermissions", DefaultValue = "", IsRequired = true)]
public String SvcPermissions {
get { return (String)this["svcPermissions"]; }
set { this["svcPermissions"] = value; }
}
}
}
主要的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using ServicesConfigSection;
namespace TestApp {
class Program {
static void Main(string[] args) {
ServicesSection svcConfig = ServicesSection.GetConfiguration();
foreach (ServiceElement thisConfig in svcConfig.Services) {
Console.WriteLine(thisConfig.SvcName);
}
}
}
}
加载 XML 时遇到的错误
System.Configuration.ConfigurationErrorsException 未处理消息=为 SecuritySettingsGroup/SecuritySettings 创建配置节处理程序时出错:无法从程序集“ConfigSecHandler”加载类型“ConfigSecHandler.ServicesSection”。(######\TestApp\bin\Debug\TestApp.vshost.exe.config 第 6 行)
虚拟主机配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Configuration section-handler declaration area. -->
<configSections>
<sectionGroup name="SecuritySettingsGroup">
<section name="SecuritySettings" type="ServicesConfigSection.ServicesSection, ServicesConfigSection" />
</sectionGroup>
<!-- Other <section> and <sectionGroup> elements.-->
</configSections>
<!-- Configuration section settings area.-->
<SecuritySettingsGroup>
<SecuritySettings>
<ServiceSecuritySettings>
<Service svcName="xStore" svcUser="user1" svcPermissions="pemission1" />
<Service svcName="xStore" svcUser="user2" svcPermissions="pemission3" />
</ServiceSecuritySettings>
</SecuritySettings>
</SecuritySettingsGroup>
</configuration>