This is a multi-part message in MIME format.
------=_NextPart_000_004B_01C68A78.14737F60
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
When creating your own configuration section it's not possible to =
enforce a specific length for a specific property.=20
Eg:
1: public class CryptoSection : ConfigurationSection 2: { =
3: #region Construction 4: 5: public =
CryptoSection() 6: { 7: } 8: 9: =
#endregion 10: 11: #region Properties 12: 13: =
[ConfigurationProperty("decryptionKey", IsRequired =3D true, IsKey =3D =
false)] 14: [StringValidator(MinLength =3D 24, MaxLength =3D =
24)] 15: public string DecryptionKey 16: { 17: =
get 18: { 19: return (string) =
this["decryptionKey"]; 20: } 21: set 22: =
{ 23: this["decryptionKey"] =3D value; 24: =
} 25: } 26: 27: #endregion 28: =
}
Then calling the following lines will throw an exception because for =
some reason when the validator gets invoked the first time =
this["decryptionKey"] will always be "" and therefore a length of 0:
1: CryptoSection cs =3D =
ConfigurationManager.GetSection("cryptoSection") as CryptoSection; 2: =
Response.Write("DecryptionKey =3D " + cs.DecryptionKey);
The exception being thrown is:
ArgumentException: The string must be at least 24 characters long.]
System.Configuration.StringValidator.Validate(Object value) +247
System.Configuration.ConfigurationProperty.Validate(Object value) +27
[ConfigurationErrorsException: The value for the property =
'decryptionKey' is not valid. The error is: The string must be at least =
24 characters long.]
=
System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean =
inputIsTrusted, FactoryRecord factoryRecord, SectionRecord =
sectionRecord, Object parentConfig, ConfigXmlReader reader, String =
filename, Int32 line) +276
...
The only work-around i have found so far is by setting in the =
ConfigurationPropertyAttribute a default value with a length of 24 (Eg: =
24 spaces), no need. There has to be a better way otherwise the the =
MinLength of the StringValidator is useless in this context because the =
value will always be a String.Empty when the validator is invoked.
--=20
Gabriel Lozano-Mor=E1n
The .NET Aficionado
http://www.pointerx.net
------=_NextPart_000_004B_01C68A78.14737F60
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.3790.2666" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>When creating your own configuration =
section it's=20
not possible to enforce a specific length for a specific property. =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Eg:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV><!-- code formatted =
by http://manoli.net/csharpformat/ -->
<STYLE type=3Dtext/css>
.csharpcode
{
font-size: small;
color: black;
font-family: Courier New , Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt=20
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</STYLE>
<DIV class=3Dcsharpcode><PRE><SPAN class=3Dlnum> 1: </SPAN> <SPAN =
class=3Dkwrd>public</SPAN> <SPAN class=3Dkwrd>class</SPAN> CryptoSection =
: ConfigurationSection</PRE><PRE><SPAN class=3Dlnum> 2: </SPAN> =
{</PRE><PRE><SPAN class=3Dlnum> 3: </SPAN> <SPAN =
class=3Dpreproc>#region</SPAN> Construction</PRE><PRE><SPAN =
class=3Dlnum> 4: </SPAN> </PRE><PRE><SPAN class=3Dlnum> 5: =
</SPAN> <SPAN class=3Dkwrd>public</SPAN> =
CryptoSection()</PRE><PRE><SPAN class=3Dlnum> 6: </SPAN> =
{</PRE><PRE><SPAN class=3Dlnum> 7: </SPAN> }</PRE><PRE><SPAN =
class=3Dlnum> 8: </SPAN> </PRE><PRE><SPAN class=3Dlnum> 9: =
</SPAN> <SPAN class=3Dpreproc>#endregion</SPAN></PRE><PRE><SPAN =
class=3Dlnum> 10: </SPAN> </PRE><PRE><SPAN class=3Dlnum> 11: =
</SPAN> <SPAN class=3Dpreproc>#region</SPAN> =
Properties</PRE><PRE><SPAN class=3Dlnum> 12: =
</SPAN> </PRE><PRE><SPAN class=3Dlnum> 13: </SPAN> =
[ConfigurationProperty(<SPAN class=3Dstr>"decryptionKey"</SPAN>, =
IsRequired =3D <SPAN class=3Dkwrd>true</SPAN>, IsKey =3D <SPAN =
class=3Dkwrd>false</SPAN>)]</PRE><PRE><SPAN class=3Dlnum> 14: </SPAN> =
[StringValidator(MinLength =3D 24, MaxLength =3D =
24)]</PRE><PRE><SPAN class=3Dlnum> 15: </SPAN> <SPAN =
class=3Dkwrd>public</SPAN> <SPAN class=3Dkwrd>string</SPAN> =
DecryptionKey</PRE><PRE><SPAN class=3Dlnum> 16: </SPAN> =
{</PRE><PRE><SPAN class=3Dlnum> 17: </SPAN> =
get</PRE><PRE><SPAN class=3Dlnum> 18: </SPAN> =
{</PRE><PRE><SPAN class=3Dlnum> 19: </SPAN> <SPAN =
class=3Dkwrd>return</SPAN> (<SPAN class=3Dkwrd>string</SPAN>) <SPAN =
class=3Dkwrd>this</SPAN>[<SPAN =
class=3Dstr>"decryptionKey"</SPAN>];</PRE><PRE><SPAN class=3Dlnum> 20: =
</SPAN> }</PRE><PRE><SPAN class=3Dlnum> 21: </SPAN> =
set</PRE><PRE><SPAN class=3Dlnum> 22: </SPAN> =
{</PRE><PRE><SPAN class=3Dlnum> 23: </SPAN> <SPAN =
class=3Dkwrd>this</SPAN>[<SPAN class=3Dstr>"decryptionKey"</SPAN>] =3D =
<SPAN class=3Dkwrd>value</SPAN>;</PRE><PRE><SPAN class=3Dlnum> 24: =
</SPAN> }</PRE><PRE><SPAN class=3Dlnum> 25: </SPAN> =
}</PRE><PRE><SPAN class=3Dlnum> 26: </SPAN> </PRE><PRE><SPAN =
class=3Dlnum> 27: </SPAN> <SPAN =
class=3Dpreproc>#endregion</SPAN></PRE><PRE><SPAN class=3Dlnum> 28: =
</SPAN> }</PRE></DIV>
<P>
<DIV><FONT face=3DArial size=3D2>Then calling the following lines will =
throw an=20
exception because for some reason when the validator gets invoked the =
first time=20
this["decryptionKey"] will always be "" and therefore a length of=20
0:</FONT></DIV>
<P><!-- code formatted by http://manoli.net/csharpformat/ -->
<DIV class=3Dcsharpcode><PRE><SPAN class=3Dlnum> 1: </SPAN> =
CryptoSection cs =3D ConfigurationManager.GetSection(<SPAN =
class=3Dstr>"cryptoSection"</SPAN>) <SPAN class=3Dkwrd>as</SPAN> =
CryptoSection;</PRE><PRE><SPAN class=3Dlnum> 2: </SPAN> =
Response.Write(<SPAN class=3Dstr>"DecryptionKey =3D "</SPAN> + =
cs.DecryptionKey);</PRE></DIV>
<P>
<DIV><FONT face=3DArial size=3D2>The exception being thrown =
is:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>ArgumentException: The string =
must be at=20
least 24 characters long.]<BR> =20
System.Configuration.StringValidator.Validate(Object value) =
+247<BR> =20
System.Configuration.ConfigurationProperty.Validate(Object value)=20
+27<BR><BR>[ConfigurationErrorsException: The value for the property=20
'decryptionKey' is not valid. The error is: The string must be at least =
24=20
characters long.]<BR> =20
System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean=20
inputIsTrusted, FactoryRecord factoryRecord, SectionRecord =
sectionRecord, Object=20
parentConfig, ConfigXmlReader reader, String filename, Int32 line)=20
+276</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>...<BR></DIV></FONT>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>The only work-around i have found so =
far is by=20
setting in the ConfigurationPropertyAttribute a default value with a =
length of=20
24 (Eg: 24 spaces), no need. There has to be a better way otherwise the =
the=20
MinLength of the StringValidator is useless in this context because the =
value=20
will always be a String.Empty when the validator is =
invoked.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT><BR><FONT face=3DArial =
size=3D2>-- <BR>Gabriel=20
Lozano-Mor=E1n<BR>The .NET Aficionado<BR></FONT><A=20
href=3D"http://www.pointerx.net"><FONT face=3DArial=20
size=3D2>http://www.pointerx.net</FONT></A></DIV></BODY></HTML>
------=_NextPart_000_004B_01C68A78.14737F60--