问题描述
我有一个正试图从web.config文件加载CustomConfigurationSection一个控制台应用程序。
I have a console application that is trying to load a CustomConfigurationSection from a web.config file.
自定义配置部分有所需的自定义配置元素。这意味着,当我加载配置部分,我希望看到一个异常如果config元素中不存在的配置。问题是,在.NET框架似乎完全无视isRequired属性。所以,当我加载配置部分,我只是创建自定义配置元素的一个实例,并设置它的配置部分。
The custom configuration section has a custom configuration element that is required. This means that when I load the config section, I expect to see an exception if that config element is not present in the config. The problem is that the .NET framework seems to be completely ignoring the isRequired attribute. So when I load the config section, I just creates an instance of the custom configuration element and sets it on the config section.
我的问题是,为什么会出现这种情况?我想GetSection()方法,以触发ConfigurationErrors异常,因为所需的元素是从配置中缺少。
My question is, why is this happening? I want the GetSection() method to fire a ConfigurationErrors exception since a required element is missing from the configuration.
下面是我的配置部分的外观。
Here is how my config section looks.
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(); }
}
}
下面是我如何加载的配置部分。
Here is how I load the config section.
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());
}
}
下面是我的配置文件的样子。
Here is what my config file looks like.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MyConfigSection" type="configurationFrameworkTestHarness.MyConfigSection, configurationFrameworkTestHarness" />
</configSections>
<MyConfigSection>
</MyConfigSection>
的
奇怪的是,如果我打开配置文件并加载第2次连胜,我会得到我期望例外。
The wierd part is that if I open the config file and load the section 2 times in a row, I will get the exception that I expect.
var configManager = OpenConfigFile(path);
var configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;
configManager = OpenConfigFile(path);
configSection = configManager.GetSection("MyConfigSection") as MyConfigSection;
如果我用上面的代码,那么该异常会火,并告诉我,MyConfigElement是必需的。问题是为什么不扔该异常的第一次??
If I use the code above, then the exception will fire and tell me that MyConfigElement is required. The question is Why is it not throwing this exception the first time??
推荐答案
我发现,这样做的最好的解决方法是通过所有嵌套属性将ConfigurationElement类型的手动迭代,并获得部分后,检查他们自己。如果一个元素是必需的,但不存在该文件我只是抛出一个ConfigurationErrorsException英寸
I found that the best workaround for this was to manually iterate through all nested properties that of the ConfigurationElement type and check them myself after getting the section. If an element is required but is not present in the file I just throw a 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);
}
}
这篇关于加载的ConfigurationSection具有所需孩子的ConfigurationElement与.net配置框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!