Issues on Validation Application Block

Topics: Validation Application Block
May 13, 2010 at 2:01 PM
Edited May 13, 2010 at 2:15 PM

I'm trying to do a proof of concept validation on a WebApplication example and I'm recieving this exception:  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The current type, Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider, is an interface and cannot be constructed. Are you missing a type mapping?

Source Error:

Server Error in '/' Application.

The current type, Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider, is an interface and cannot be constructed. Are you missing a type mapping?

Line 18:         protected void Button1_Click(object sender, EventArgs e)
Line 19:         {
Line 20:             var validator = ValidationFactory.CreateValidatorFromAttributes<Citizen>();
Line 21:         }
Line 22:     }


Source File: C:\Users\Administrator\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Default.aspx.cs    Line: 20

Stack Trace:

[InvalidOperationException: The current type, Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider, is an interface and cannot be constructed. Are you missing a type mapping?]
   Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) +268
   BuildUp_Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider(IBuilderContext ) +69
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +309
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +262
   Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +229
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +90
   BuildUp_Microsoft.Practices.EnterpriseLibrary.Validation.AttributeValidatorFactory(IBuilderContext ) +189
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +309
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +262
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +377

[ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Validation.AttributeValidatorFactory", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:

  Resolving Microsoft.Practices.EnterpriseLibrary.Validation.AttributeValidatorFactory,(none)
  Resolving parameter "instrumentationProvider" of constructor Microsoft.Practices.EnterpriseLibrary.Validation.AttributeValidatorFactory(Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider instrumentationProvider)
    Resolving Microsoft.Practices.EnterpriseLibrary.Validation.Instrumentation.IValidationInstrumentationProvider,(none)
]
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +440
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides) +45
   Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) +43
   Microsoft.Practices.Unity.UnityServiceLocator.DoGetInstance(Type serviceType, String key) +134
   Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) +74

[ActivationException: Activation error occured while trying to get instance of type AttributeValidatorFactory, key ""]
   Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) +154
   Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance() +107
   Microsoft.Practices.EnterpriseLibrary.Validation.ValidationFactory.get_DefaultAttributeValidatorFactory() +54
   Microsoft.Practices.EnterpriseLibrary.Validation.ValidationFactory.CreateValidatorFromAttributes() +31
   WebApplication1._Default.Button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Default.aspx.cs:20
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

The citizen class is very simple:

public class Citizen
    {
        #region Properties

        /// <summary>
        /// Gets or sets FirstName.
        /// </summary>
        [NotNullValidator]
        [StringLengthValidator(1, 250, MessageTemplate="Invalid lenght")]
        public string FirstName { get; set; }

        #endregion
    }
What does Unity here? I think that the method CreateValidatorFromAttribute it based on attribute configuration, isn't it?
Thnaks!!!
May 13, 2010 at 2:36 PM

Another example more easy:

namespace ConsoleApplication1
{
    public class Citizen
    {
        #region Properties

        /// <summary>
        /// Gets or sets FirstName.
        /// </summary>
        [StringLengthValidator(1, 50,
               MessageTemplate = "First Name must be between 1 and 50 characters")]
        public string FirstName { get; set; }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            var validator = ValidationFactory.CreateValidatorFromAttributes<Citizen>();
        }
    }
}

The same exception ;)

Thanks!!! 

May 14, 2010 at 1:48 AM

To answer your question on what Unity does here, EntLib 5 uses dependency injection mechanism which is by default, Unity, to create and manage the lifetime of entlib objects.

However, I wasn't able to repro your error, I copied the exact code and have references to the Validation and System.ComponentModel.DataAnnotations assemblies.  Was there an inner exception?

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

May 14, 2010 at 7:32 AM
Edited May 14, 2010 at 7:38 AM

I've been resolved the problem put an assembly attribute Copy Local = true on Visual Studio 2010, but I think that this is a bug in EL 5.0 isn't it?

Thanks!!!

May 14, 2010 at 8:03 AM

Ah, so you're referencing Validation assembly from GAC. Yes, here is the link to the logged work item, another workaround is available if you want to continue referencing it from GAC - http://entlib.codeplex.com/WorkItem/View.aspx?WorkItemId=26903

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com