I have a class:
WebApiInstaller : IWindsorInstaller
which contains this:
container.Register(
Classes
.FromThisAssembly()
.BasedOn(typeof(AbstractValidator<>))
.WithService
.Base());
var container = new WindsorContainer();
container.Install(new WebApiInstaller());
GlobalConfiguration.Configuration.DependencyResolver = new WindsorHttpDependencyResolver(container);
in my global.cs file I currently use this:
var container = new WindsorContainer();
container.Install(new WebApiInstaller());
GlobalConfiguration.Configuration.DependencyResolver = new WindsorHttpDependencyResolver(container);
FluentValidationModelValidatorProvider.Configure(GlobalConfiguration.Configuration, provider => provider.ValidatorFactory = new WindsorFluentValidatorFactory(container.Kernel));
Here WindsorFluentValidatorFactory looks as follows:
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;
}
}
My endpoint looks like this:
public IHttpActionResult AddPointGivenGeoJsonPointDto([FromBody] Bla blaDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
}
Bla is decorated like this:
[Validator(typeof(BlaValidator))]
public class Bla
and the validator looks like this:
public class BlaValidator : AbstractValidator<Bla>
Unfortunately, the ModelState is never invalid despite invalid objects being passed in an integration test. Can anyone see anything wrong with my windsor.castle registration, which I think does not work.
This is really hard to diagnose without stepping through the code in debug mode.
Its been a while since I've used castle windsor but one of the things i would suggest is to put a break point in your global.cs and add a reference to the container object in your watch window, after it has been configured through the WindsorFluentValidatorFactory class.
By doing this you can examine the container and all of its registered services. I know that with Castle Windsor, it also has a list of the services it failed to register and the reasons why they failed.
From my experience, it was generally the case that there was a missing dependency in the chain, and if one dependency doesn't register properly, any other dependency referencing it will fail also.
Hope this helps.