web config - Implementing a cutom configuration section with a collection of configurationSection, getting error Unrecognized element -


i trying have list of smtp sections achieve switching between them send out email. starting this example, config file looks this,

<?xml version="1.0" encoding="utf-8"?> <emailconfiguration>    <defaultsettings>     <setting key="defaultfromemail" value="bishwas.gautam@xxx.com"/>     <setting key="defaultsmptsectionname" value="smtp_default"/>   </defaultsettings>    <smtpsettingslist>     <!--default mail drop-->     <smtp_default deliverymethod="specifiedpickupdirectory">       <specifiedpickupdirectory pickupdirectorylocation="c:\xxx" />     </smtp_default>      <!--third party smtp service-->     <smtp_aws_ses deliverymethod="network">       <network host="email-smtp.abc.com" port="2127" enablessl="true"              username="xyz" password="*"/>     </smtp_aws_ses>      <smtp_customclient deliverymethod="network">       <network host="*" port="*" enablessl="*"              username="*" password="*"/>     </smtp_customclient>    </smtpsettingslist>  </emailconfiguration> 

this referenced web.config this:

<configsections>      <section name="smtp_default" type="system.net.configuration.smtpsection"/>     <section name="smtp_aws_ses" type="system.net.configuration.smtpsection"/>     <section name="smtp_customclient" type="system.net.configuration.smtpsection"/>          <section name="emailconfiguration" type="pro.configurations.emailconfiguration, pro.infrastructure, version=1.0.0.0, culture=neutral"/>          </configsections>   <emailconfiguration configsource="emailconfiguration.config"/> 

i have corresponding configuration class emailconfiguration looks like:

public class emailconfiguration : configurationsection, iemailconfiguration {     public emailconfiguration()     {      }      [configurationproperty("defaultsettings", isrequired = true)]     private settingelementcollection generalsettings     {         { return (settingelementcollection)this["defaultsettings"]; }     }      [configurationproperty("smtpsettingslist", isrequired = false)]     public smtpsettingslist smtpsettingslist     {         { return (smtpsettingslist) this["smtpsettingslist"]; }     }      public string defaultfromemail     {                 {             return generalemailsettings[emailconfigurationkeys.defaultfromemail];         }     }      public string defaultsmptsectionname     {                 {             return generalemailsettings[emailconfigurationkeys.defaultsmptsectionname];         }      }      public dictionary<string, string> generalemailsettings => generalsettings.settings;     public dictionary<string, smtpsection> smtpsections => smtpsettingslist.smtpsections;       public smtpsection getsmtpsection(string sectionname)     {         smtpsection result = null;          smtpsections.trygetvalue(sectionname, out result);          return result;     } }  [configurationcollection(typeof(configurationsectioncollection))] public class smtpsettingslist : configurationelementcollection {     protected override configurationelement createnewelement()     {         return new smtpsection();     }      protected override object getelementkey(configurationelement element)     {         return ((smtpsection)element).sectioninformation.name;     }      public dictionary<string, smtpsection> smtpsections     {                 {             return this.cast<smtpsection>().tolist().todictionary(e => e.sectioninformation.name, e => e);         }     }      public override configurationelementcollectiontype collectiontype     {         { return configurationelementcollectiontype.basicmap; }     }  } 

but when try access configuration this:

 var emailconfiguration = (emailconfiguration)configurationmanager.getsection("emailconfiguration"); 

i getting error unrecognized element 'smtp_default'.

what doing wrong?

thanks.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -