我有一个控制台应用程序,它试图从web.config文件加载CustomConfigurationSection。
自定义配置节有一个必需的自定义配置元素。这意味着当我加载配置节时,如果该配置元素不在配置中,我会看到一个异常。问题是.NET框架似乎完全忽略了isRequired属性。因此,当我加载配置节时,我只是创建了自定义配置元素的一个实例,并在配置节上设置它。
我的问题是,为什么会发生这种情况?我希望GetSection()方法触发一个ConfigurationErrors异常,因为配置中缺少必需的元素。
下面是我的配置节的样子。
public class MyConfigSection : ConfigurationSection
{
[ConfigurationProperty("MyConfigElement", IsRequired = true)]
public MyConfigElement MyElement
{
get { return (MyConfigElement) this["MyConfigElement"]; }
}
}
public class MyConfigElement : ConfigurationElement
{
[ConfigurationProperty("MyAttribute", IsRequired = true)]
public string MyAttribute
{
get { return this["MyAttribute"].ToString(); }
}
}下面是我加载配置节的方法。
class Program
{
public static Configuration OpenConfigFile(string configPath)
{
var configFile = new FileInfo(configPath);
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}
static void Main(string[] args)
{
try{
string path = @"C:\Users\vrybak\Desktop\Web.config";
var configManager = OpenConfigFile(path);
var configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;
MyConfigElement elem = configSection.MyElement;
} catch (ConfigurationErrorsException ex){
Console.WriteLine(ex.ToString());
}
}下面是我的配置文件的样子。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MyConfigSection" type="configurationFrameworkTestHarness.MyConfigSection, configurationFrameworkTestHarness" />
</configSections>
<MyConfigSection>
</MyConfigSection>奇怪的是,如果我打开配置文件并连续2次加载该部分,我将得到我所期望的异常。
var configManager = OpenConfigFile(path);
var configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;
configManager = OpenConfigFile(path);
configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;如果我使用上面的代码,那么异常将被触发并告诉我MyConfigElement是必需的。问题是,为什么它不在第一次抛出这个异常?
发布于 2010-03-22 20:31:06
我发现最好的解决方法是手动迭代所有ConfigurationElement类型的嵌套属性,并在获得部分后亲自检查它们。如果某个元素是必需的,但不在文件中,我只会抛出一个ConfigurationErrorsException。
这是我的代码。
private void ProcessMissingElements(ConfigurationElement element)
{
foreach (PropertyInformation propertyInformation in element.ElementInformation.Properties)
{
var complexProperty = propertyInformation.Value as ConfigurationElement;
if (complexProperty == null)
continue;
if (propertyInformation.IsRequired && !complexProperty.ElementInformation.IsPresent)
throw new ConfigurationErrorsException("ConfigProperty: [{0}] is required but not present".FormatStr(propertyInformation.Name));
if (!complexProperty.ElementInformation.IsPresent)
propertyInformation.Value = null;
else
ProcessMissingElements(complexProperty);
}
}发布于 2010-03-19 14:36:21
MS论坛中的Eric has answered this
引用他的回答:
ConfigurationPropertyAttribute的IsRequired成员在应用于子对象(从ConfigurationElement派生)时不起作用
发布于 2010-03-22 20:12:43
您是否尝试将其直接赋值给正确类型的变量,即MyConfigSection而不是var?这是我能看到的两行代码之间的唯一区别。(即在第二行中,var现在采用了一个特定的类型)。
https://stackoverflow.com/questions/2470996
复制相似问题