Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Issues setting up FluentValidation with Castle.Windsor

$
0
0
Hi, I have an asp.net MVC 4.5 application with Castle.Windsor 3.2.2 as DI and I'm trying to add FluentValidation version 5.0.0.1 for the first time.

I created the factory inheriting from ValidatorFactoryBase

public class WindsorFluentValidatorFactory : ValidatorFactoryBase
{
    private readonly IKernel _kernel;

    public WindsorFluentValidatorFactory(IKernel kernel)
    {
        _kernel = kernel;
    }

    public override IValidator CreateInstance(Type validatorType)
    {
        return _kernel.HasComponent(validatorType)
                     ? _kernel.Resolve<IValidator>(validatorType)
                     : null;
    }
}
Also created the installer for Castle:

public class FluentValidatorsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Classes
                .FromThisAssembly()
                .BasedOn(typeof(IValidator<>))
                .WithService
                .Base());
    }
}
Now, in the Global.asax, I tried (following documentation on web) to hook things like this:

var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider(new WindsorFluentValidatorFactory(container.Kernel));
ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);

However, the second line gives the following error:

"The best overloaded method match for 'System.Collections.ObjectModel.Collection<System.Web.Mvc.ModelValidatorProvider>.Add(System.Web.Mvc.ModelValidatorProvider)' has some invalid arguments
Argument 1: cannot convert from 'FluentValidation.Mvc.WebApi.FluentValidationModelValidatorProvider' to 'System.Web.Mvc.ModelValidatorProvider'"

So, it seems that this way of doing it is not working on the versions I use.

I also tried to hook this using the Configure method doing this:
FluentValidationModelValidatorProvider.Configure(x => x.ValidatorFactory = new WindsorFluentValidatorFactory(container.Kernel));

However, it's not working. Debugging I checked at ModelValidatorProviders.Providers and there is no new provider being add with the Configure method.

Any ideas on what I might be doing wrong?
Thanks,

Andres

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>