ValBlock Invalid Cast: GetConfigurationSourceSection()

Topics: Validation Application Block
Nov 30, 2008 at 1:13 PM
I'm getting an invalid cast exception when I use the Enteprise Library Validation Block. I'm using a configuration file for the validation settings.
 I have include the code that causes the exception, web.config settings and exception details at the bottom. I was using Ent 3.1 with VS2008 3.5 but switched to Ent Lib 4.0 and ran into this problem. I updated all the references; even deleted all the bin folders but no luck. Please help!

web.config excerpts
<section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<
section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

Validation Section of web.config

 <validation>
    <type assemblyName="Company.SupplierWeb.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      name="Company.SupplierWeb.Common.Part">
      <ruleset name="PartValidation">
        <properties>
          <property name="LowerControlLimit">
            <validator lowerBound="1" lowerBoundType="Inclusive" upperBound="1"
              upperBoundType="Ignore" negated="false" messageTemplate="Must be Greater Than One"
              messageTemplateResourceName="" messageTemplateResourceType=""
              tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="Range Validator" />
          </property>
        </properties>
      </ruleset>
    </type>
  </validation>



Exception that is thrown when the following code is executed.
ValidationResults

 

_partValidationResults = Validation.ValidateFromConfiguration<Part>(part, "PartValidation");
Saved Exception Info
  message.ToString() "Error occured while processing transaction: 12345 \r\nTransaction was processed at 11/30/2008\r\nMessage: Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection' to type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection'.\r\nInnerException \r\nStack Trace:    at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection.GetConfigurationSourceSection()\r\n   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceFactory.Create()\r\n   at Microsoft.Practices.EnterpriseLibrary.Validation.ValidationFactory.CreateValidatorFromConfiguration[T](String ruleset)\r\n   at Microsoft.Practices.EnterpriseLibrary.Validation.Validation.ValidateFromConfiguration[T](T target, String[] rulesets)\r\n   at Company.SupplierWeb.Application.PartValidator.ValidatePart(Part part) in C:\\Documents and Settings\\username\\Desktop\\Projects\\SupplierWebServices\\1.1\\Application\\Validators\\PartValidator.cs:line 75\r\n   at Company.SupplierWeb.Application.SupplierPartListComponent.SavePartList(List`1 Parts, Transaction transactionData) in C:\\Documents and Settings\\username\\Desktop\\Projects\\SupplierWebServices\\1.1\\Application\\Components\\SupplierPartListComponent.cs:line 78\r\n   at Company.SupplierWeb.Application.SupplierService.SaveSupplierWebMinMaxPartData(List`1 Parts, Transaction transactionData) in C:\\Documents and Settings\\username\\Desktop\\Projects\\SupplierWebServices\\1.1\\Application\\Services\\SupplierService.cs:line 39" string

Dec 2, 2008 at 9:04 AM
Edited Dec 2, 2008 at 9:05 AM
Hi,

Maybe you can try creating a sample application which has a validation functionality as you've described, but on plain Ent lib 4.0. Then try to compare the config to which you've just updated the references. Also, send me your original solution so i can have a closer look.


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 14, 2009 at 5:01 AM
Hi,

This is because Enterprise Library 4.0 adds the following to your machine.config file:

 <section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

I think if you have mutliple versions of the Enteprise Library installed it gets confused.

As I work around I removed this configuration.

John
Feb 16, 2009 at 12:58 AM
Thanks for the Info.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com