Tuesday, June 11, 2013

Sharepoint 2013 + EntLib 5 Validation block

Using Enterprise Library 5 with Sharepoint 2013 could be a tricky. A strange exception is thrown from Validation block of EntLib when it's used from Sharepoint:

Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type ValidatorFactory, key "" ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Validation.ValidatorFactory", name = "(none)".  Exception occurred while: while resolving.  Exception is: InvalidOperationException - The type ValidatorFactory does not have an accessible constructor.  -----------------------------------------------  At the time of the exception, the container was:      Resolving Microsoft.Practices.EnterpriseLibrary.Validation.ValidatorFactory,(none)   ---> System.InvalidOperationException: The type ValidatorFactory does not have an accessible constructor.

The solution is to add the following configuration elements into web.config:

1. Add a section declaration into <configSections>

<section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.1.508.0, Culture=neutral, PublicKeyToken=3602034a0e4e3f13" />

2. Add the section into <configuration>

<typeRegistrationProvidersConfiguration>
    <remove name="Validation" />
    <add name="Validation" providerType="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationTypeRegistrationProvider, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.1.508.0, Culture=neutral, PublicKeyToken=3602034a0e4e3f13" />
  </typeRegistrationProvidersConfiguration>