I have following class extended ApplicationSettingsBase as below
public class SMTPServerConfig : ApplicationSettingsBase
{
private static SMTPServerConfig instance = (SMTPServerConfig)Synchronized(new SMTPServerConfig());
public static SMTPServerConfig Instance { get { return instance; } }
[ApplicationScopedSetting]
[DefaultSettingValue("")]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
public List<SMTPServerConfigItem> SMTPServers
{
get
{
return ((List<SMTPServerConfigItem>)(this["SMTPServers"]));
}
}
[ApplicationScopedSetting]
[DefaultSettingValue("")]
[SettingsSerializeAs(System.Configuration.SettingsSerializeAs.String)]
public string TestProp
{
get
{
return ((string)(this["TestProp"]));
}
}
}
[Serializable]
public class SMTPServerConfigItem
{
public string SMTPHost { get; set; }
public string SMTPPort { get; set; }
public string SMTPAccount { get; set; }
public string SMTPPassword { get; set; }
}
Here is my App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Example.Config.SMTPServerConfig" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Example.Config.SMTPServerConfig>
<setting name="TestProp" serializeAs="String">
<value>123123</value>
</setting>
<setting name="SMTPServers" serializeAs="Xml">
<value>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>[email protected]</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>[email protected]</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
</value>
</setting>
</Example.Config.SMTPServerConfig>
</applicationSettings>
</configuration>
When i use SMTPServerConfig.Instance.TestProp it will show result is "123123" (it's true value, it mean i have correct config's schema for it), but SMTPServerConfig.Instance.SMTPServers.Count always zero. Where i'm wrong in code or config file ?
After a hours of research, i found a question in stackoverflow then i know where i'm wrong. I was missing a xml tag in app.config file. Question: Trying to create a custom partial settings file
Then now i will correct my App.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Example.Config.SMTPServerConfig" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Example.Config.SMTPServerConfig>
<setting name="TestProp" serializeAs="String">
<value>123123</value>
</setting>
<setting name="SMTPServers" serializeAs="Xml">
<value>
<ArrayOfSMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>[email protected]</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
<SMTPServerConfigItem>
<SMTPHost>smtp.example.com</SMTPHost>
<SMTPPort>123</SMTPPort>
<SMTPAccount>[email protected]</SMTPAccount>
<SMTPPassword>123456</SMTPPassword>
</SMTPServerConfigItem>
</ArrayOfSMTPServerConfigItem>
</value>
</setting>
</Example.Config.SMTPServerConfig>
</applicationSettings>
</configuration>
After add ArrayOfSMTPServerConfigItem tag, it will solve this problem.