{"id":548,"date":"2010-03-05T03:28:40","date_gmt":"2010-03-05T10:28:40","guid":{"rendered":"http:\/\/kennyw.com\/work\/548"},"modified":"2010-03-05T03:28:40","modified_gmt":"2010-03-05T10:28:40","slug":"argumentexception-the-string-must-be-at-least-xxx-characters-long-2","status":"publish","type":"post","link":"https:\/\/kennyw.com\/?p=548","title":{"rendered":"ArgumentException: The string must be at least XXX characters long"},"content":{"rendered":"<p>I hit this obtuse exception the other day. The stack had System.Configuration code on it, which is the only way I managed to track it down to a StringValidator attribute. A few things that would have been helpful:<\/p>\n<ol>\n<li>Having the name of the culprit in the exception string (i.e. The string \u2018foo\u2019 must be at least 24 characters long.) <\/li>\n<li>Including the configuration element of which string \u2018foo\u2019 is a property of (i.e. The string \u2018foo\u2019 must be at least 24 characters long to be used for property \u2018bar\u2019 on element \u2018baz\u2019.) <\/li>\n<\/ol>\n<p>Turns out that this was not actually a .config file error. After subclassing <span style=\"font-family: &quot;Courier New&quot;; color: #2b91af\">StringValidator<\/span> and creating a <span style=\"font-family: &quot;Courier New&quot;; color: #2b91af\">ConfigurationValidatorAttribute<\/span> to bridge into the declarative system, I discovered that the exception was being thrown while validating the default value. <\/p>\n<p>I had naively thought that if you have a required ConfigurationProperty, then you do not need to setup a default value. I was wrong. I discovered that this is a <a href=\"http:\/\/dotnet.itags.org\/dotnet\/629733\/\">known<\/a>&#160;<a href=\"https:\/\/connect.microsoft.com\/VisualStudio\/feedback\/details\/120022\/stringvalidator-attribute-in-configurationsection-validates-and-fails-before-reading-config-file\">bug<\/a> in the .Net Configuration validation framework that has been resolved \u201cBy Design\u201d. <\/p>\n<p>I completely understand why they want to run the validators on default values, but the framework really should check if a property is required and suppress default value validation in that case. As it is, the default value is never used, and the framework properly throws an exception if the required property isn\u2019t explicitly set by the user. <\/p>\n<p>As a result, the following is the \u201ccorrect\u201d way to have a required property that works with the config validation framework:<\/p>\n<blockquote>\n<p><span style=\"font-family: &quot;Courier New&quot;\">[<span style=\"color: #2b91af\">ConfigurationProperty<\/span>(namePropertyName, IsRequired = <span style=\"color: blue\">true<\/span>,         <br \/><span style=\"color: green\">&#160;&#160; \/\/ We need to set a default value for our string validator to work&#160; <br \/>&#160;&#160; <\/span>DefaultValue = <span style=\"color: #a31515\">&quot;dummy&quot;<\/span>)]&#160; <br \/>[<span style=\"color: #2b91af\">StringValidator<\/span>(MinLength = 1)]         <br \/><span style=\"color: blue\">public<\/span> <span style=\"color: blue\">string<\/span> Name { &#8230; } <\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I hit this obtuse exception the other day. The stack had System.Configuration code on it, which is the only way I managed to track it down to a StringValidator attribute. A few things that would have been helpful: Having the name of the culprit in the exception string (i.e. The string \u2018foo\u2019 must be at [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[13,34,73],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-work","tag-argumentexception","tag-errorspotting","tag-stringvalidator"],"_links":{"self":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=548"}],"version-history":[{"count":0,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/548\/revisions"}],"wp:attachment":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}