ConfigurationManager 立即给出 TypeInitializationException
ConfigurationManager gives a TypeInitializationException instantly
我正在制作一个需要读取其配置文件以进行设置的应用程序。
我在 App_config 文件中定义了如下设置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sswcomm" type="SSWAgent.SSWConfig" />
<section name="folders" type="SSWAgent.SSWConfigFolderCollection" />
<section name="folder" type="SSWAgent.SSWConfigFolderElement" />
</configSections>
<sswcomm id="SC_SSWTEST1" connectionInterval="5000">
<folders>
<folder id="OUTGOINGATOTEST2" enable="true" includeSubFolders="true" fromPath="C:\temp\a\NET\Outgoing_A" wildcard="*.*" recipientId="C_SSWTEST2" />
<folder id="OUTGOINGBTOTEST3" enable="true" includeSubFolders="true" fromPath="C:\temp\a\NET\Outgoing_B" wildcard="*.*" recipientId="C_SSWTEST3" />
</folders>
</sswcomm>
</configuration>
C# 代码如下:
public sealed class SSWConfig : ConfigurationSection
{
private static SSWConfig settings = ConfigurationManager.GetSection("sswcomm") as SSWConfig;
public static SSWConfig Settings
{
get { return settings; }
}
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string Id
{
get { return (string)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("connectionInterval", DefaultValue = "", IsKey = true, IsRequired = true)]
public int ConnectionInterval
{
get { return (int)this["connectionInterval"]; }
set { this["connectionInterval"] = value; }
}
[ConfigurationProperty("folders", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
public SSWConfigFolderCollection Folders
{
get { return base["folders"] as SSWConfigFolderCollection; }
}
}
public sealed class SSWConfigFolderCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new SSWConfigFolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SSWConfigFolderElement)element).Id;
}
new public SSWConfigFolderElement this[string Id]
{
get { return (SSWConfigFolderElement)base.BaseGet(Id); }
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
public SSWConfigFolderElement this[int index]
{
get { return (SSWConfigFolderElement)base.BaseGet(index); }
}
protected override string ElementName
{
get
{
return "folder";
}
}
}
public sealed class SSWConfigFolderElement : ConfigurationElement
{
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string Id
{
get { return (string)base["id"]; }
set { base["id"] = value; }
}
[ConfigurationProperty("enable", DefaultValue = "true", IsKey = false, IsRequired = true)]
public bool Enable
{
get { return (string)base["enable"] == "true"; }
set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("includeSubFolders", DefaultValue = "true", IsKey = false, IsRequired = true)]
public bool IncludeSubFolders
{
get { return (string)base["includeSubFolders"] == "true"; }
set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("fromPath", DefaultValue = "", IsKey = false, IsRequired = true)]
public string FromPath
{
get { return (string)base["fromPath"]; }
set { base["fromPath"] = value; }
}
[ConfigurationProperty("wildcard", DefaultValue = "*.*", IsKey = false, IsRequired = true)]
public string Wildcard
{
get { return (string)base["wildcard"]; }
set { base["wildcard"] = value; }
}
[ConfigurationProperty("recipientId", DefaultValue = "", IsKey = false, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string RecipientId
{
get { return (string)base["recipientId"]; }
set { base["recipientId"] = value; }
}
}
现在,每当我调用 SSWConfig
并将其放入变量中时,我会立即得到一个 TypeInitializationException
异常。我认为是因为 GetSection
部分 returns null
。这是因为我在 App.config 文件中犯了错误吗?
带有嵌套元素的 app.config 对我来说是新的,所以可能是我定义错误。
只是发布我的解决方案。
显然我让它变得比实际更难。我从头开始,经过一些修改后我的设置被读取。
我遇到的另一个错误是我从未真正阅读配置文件(哎呀),认为它会在调用配置时自动发生 class。
Class:
// <sswcomm></sswcomm> settings
public class SSWConfigSSWComm : ConfigurationSection
{
[ConfigurationProperty("id")]
public string Id
{
get { return (string)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("connectionInterval")]
public int ConnectionInterval
{
get { return (int)this["connectionInterval"]; }
set { this["connectionInterval"] = value; }
}
[ConfigurationProperty("toPath")]
public string ToPath
{
get { return (string)this["toPath"]; }
set { this["toPath"] = value; }
}
[ConfigurationProperty("folders")]
[ConfigurationCollection(typeof(SSWConfigFolderElement), AddItemName = "folder")]
public SSWConfigFolderElementCollection sswConfigFolders
{
get { return (SSWConfigFolderElementCollection)base["folders"]; }
}
}
// <folder> settings
[ConfigurationCollection(typeof(SSWConfigFolderElement))]
public class SSWConfigFolderElementCollection : ConfigurationElementCollection
{
new public SSWConfigFolderElement this[string name]
{
get { return (SSWConfigFolderElement)base.BaseGet(name); }
}
public SSWConfigFolderElement this[int index]
{
get { return (SSWConfigFolderElement)base.BaseGet(index); }
}
protected override ConfigurationElement CreateNewElement()
{
return new SSWConfigFolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SSWConfigFolderElement)element).Id;
}
}
public class SSWConfigFolderElement : ConfigurationElement
{
[ConfigurationProperty("id")]
public string Id
{
get { return (string)base["id"]; }
set { base["id"] = value; }
}
[ConfigurationProperty("enable")]
public bool Enable
{
get { return (string)base["enable"] == "true"; }
set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("includeSubFolders")]
public bool IncludeSubFolders
{
get { return (string)base["includeSubFolders"] == "true"; }
set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("fromPath")]
public string FromPath
{
get { return (string)base["fromPath"]; }
set { base["fromPath"] = value; }
}
[ConfigurationProperty("wildcard")]
public string Wildcard
{
get { return (string)base["wildcard"]; }
set { base["wildcard"] = value; }
}
[ConfigurationProperty("recipientId")]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string RecipientId
{
get { return (string)base["recipientId"]; }
set { base["recipientId"] = value; }
}
}
收集数据
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SSWConfigSSWComm sc = (SSWConfigSSWComm)conf.GetSection("sswcomm");
string Id = sc.Id;
long lngInterval = sc.ConnectionInterval;
string toPath = sc.ToPath;
SSWConfigFolderElementCollection fec = sc.sswConfigFolders;
foreach (SSWConfigFolderElement fe in fec)
{
string fromPath = fe.FromPath;
string wildcard = fe.Wildcard;
bool includeSubFolders = fe.IncludeSubFolders;
string recipientId = fe.RecipientId;
sp.GetFiles(Id, recipientId, fromPath, toPath, wildcard, includeSubFolders);
}
希望这对以后的人有所帮助。
我正在制作一个需要读取其配置文件以进行设置的应用程序。
我在 App_config 文件中定义了如下设置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sswcomm" type="SSWAgent.SSWConfig" />
<section name="folders" type="SSWAgent.SSWConfigFolderCollection" />
<section name="folder" type="SSWAgent.SSWConfigFolderElement" />
</configSections>
<sswcomm id="SC_SSWTEST1" connectionInterval="5000">
<folders>
<folder id="OUTGOINGATOTEST2" enable="true" includeSubFolders="true" fromPath="C:\temp\a\NET\Outgoing_A" wildcard="*.*" recipientId="C_SSWTEST2" />
<folder id="OUTGOINGBTOTEST3" enable="true" includeSubFolders="true" fromPath="C:\temp\a\NET\Outgoing_B" wildcard="*.*" recipientId="C_SSWTEST3" />
</folders>
</sswcomm>
</configuration>
C# 代码如下:
public sealed class SSWConfig : ConfigurationSection
{
private static SSWConfig settings = ConfigurationManager.GetSection("sswcomm") as SSWConfig;
public static SSWConfig Settings
{
get { return settings; }
}
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string Id
{
get { return (string)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("connectionInterval", DefaultValue = "", IsKey = true, IsRequired = true)]
public int ConnectionInterval
{
get { return (int)this["connectionInterval"]; }
set { this["connectionInterval"] = value; }
}
[ConfigurationProperty("folders", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
public SSWConfigFolderCollection Folders
{
get { return base["folders"] as SSWConfigFolderCollection; }
}
}
public sealed class SSWConfigFolderCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new SSWConfigFolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SSWConfigFolderElement)element).Id;
}
new public SSWConfigFolderElement this[string Id]
{
get { return (SSWConfigFolderElement)base.BaseGet(Id); }
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
public SSWConfigFolderElement this[int index]
{
get { return (SSWConfigFolderElement)base.BaseGet(index); }
}
protected override string ElementName
{
get
{
return "folder";
}
}
}
public sealed class SSWConfigFolderElement : ConfigurationElement
{
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string Id
{
get { return (string)base["id"]; }
set { base["id"] = value; }
}
[ConfigurationProperty("enable", DefaultValue = "true", IsKey = false, IsRequired = true)]
public bool Enable
{
get { return (string)base["enable"] == "true"; }
set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("includeSubFolders", DefaultValue = "true", IsKey = false, IsRequired = true)]
public bool IncludeSubFolders
{
get { return (string)base["includeSubFolders"] == "true"; }
set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("fromPath", DefaultValue = "", IsKey = false, IsRequired = true)]
public string FromPath
{
get { return (string)base["fromPath"]; }
set { base["fromPath"] = value; }
}
[ConfigurationProperty("wildcard", DefaultValue = "*.*", IsKey = false, IsRequired = true)]
public string Wildcard
{
get { return (string)base["wildcard"]; }
set { base["wildcard"] = value; }
}
[ConfigurationProperty("recipientId", DefaultValue = "", IsKey = false, IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string RecipientId
{
get { return (string)base["recipientId"]; }
set { base["recipientId"] = value; }
}
}
现在,每当我调用 SSWConfig
并将其放入变量中时,我会立即得到一个 TypeInitializationException
异常。我认为是因为 GetSection
部分 returns null
。这是因为我在 App.config 文件中犯了错误吗?
带有嵌套元素的 app.config 对我来说是新的,所以可能是我定义错误。
只是发布我的解决方案。
显然我让它变得比实际更难。我从头开始,经过一些修改后我的设置被读取。
我遇到的另一个错误是我从未真正阅读配置文件(哎呀),认为它会在调用配置时自动发生 class。
Class:
// <sswcomm></sswcomm> settings
public class SSWConfigSSWComm : ConfigurationSection
{
[ConfigurationProperty("id")]
public string Id
{
get { return (string)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("connectionInterval")]
public int ConnectionInterval
{
get { return (int)this["connectionInterval"]; }
set { this["connectionInterval"] = value; }
}
[ConfigurationProperty("toPath")]
public string ToPath
{
get { return (string)this["toPath"]; }
set { this["toPath"] = value; }
}
[ConfigurationProperty("folders")]
[ConfigurationCollection(typeof(SSWConfigFolderElement), AddItemName = "folder")]
public SSWConfigFolderElementCollection sswConfigFolders
{
get { return (SSWConfigFolderElementCollection)base["folders"]; }
}
}
// <folder> settings
[ConfigurationCollection(typeof(SSWConfigFolderElement))]
public class SSWConfigFolderElementCollection : ConfigurationElementCollection
{
new public SSWConfigFolderElement this[string name]
{
get { return (SSWConfigFolderElement)base.BaseGet(name); }
}
public SSWConfigFolderElement this[int index]
{
get { return (SSWConfigFolderElement)base.BaseGet(index); }
}
protected override ConfigurationElement CreateNewElement()
{
return new SSWConfigFolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SSWConfigFolderElement)element).Id;
}
}
public class SSWConfigFolderElement : ConfigurationElement
{
[ConfigurationProperty("id")]
public string Id
{
get { return (string)base["id"]; }
set { base["id"] = value; }
}
[ConfigurationProperty("enable")]
public bool Enable
{
get { return (string)base["enable"] == "true"; }
set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("includeSubFolders")]
public bool IncludeSubFolders
{
get { return (string)base["includeSubFolders"] == "true"; }
set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("fromPath")]
public string FromPath
{
get { return (string)base["fromPath"]; }
set { base["fromPath"] = value; }
}
[ConfigurationProperty("wildcard")]
public string Wildcard
{
get { return (string)base["wildcard"]; }
set { base["wildcard"] = value; }
}
[ConfigurationProperty("recipientId")]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\", MinLength = 1, MaxLength = 256)]
public string RecipientId
{
get { return (string)base["recipientId"]; }
set { base["recipientId"] = value; }
}
}
收集数据
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SSWConfigSSWComm sc = (SSWConfigSSWComm)conf.GetSection("sswcomm");
string Id = sc.Id;
long lngInterval = sc.ConnectionInterval;
string toPath = sc.ToPath;
SSWConfigFolderElementCollection fec = sc.sswConfigFolders;
foreach (SSWConfigFolderElement fe in fec)
{
string fromPath = fe.FromPath;
string wildcard = fe.Wildcard;
bool includeSubFolders = fe.IncludeSubFolders;
string recipientId = fe.RecipientId;
sp.GetFiles(Id, recipientId, fromPath, toPath, wildcard, includeSubFolders);
}
希望这对以后的人有所帮助。