I am trying to use Ninject FluentValidation and am having a weird issue -- when I submit a form, the constructor for the validator is being called 32 (!) times. If I remove the configuration, the constructor is only called once (but of course no injection takes place). The only thing I can think of is I should be setting up the factory somewhere other than NinjectWebCommon, but I don't see anywhere else I have access to the kernel to do so.
Any ideas why the validator is firing so many times?
In RegisterServices() in NinjectWebCommon.cs:
NinjectValidatorFactory ninjectValidatorFactory = new NinjectValidatorFactory(kernel);
FluentValidationModelValidatorProvider.Configure(provider => provider.ValidatorFactory = ninjectValidatorFactory);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
In the validation resolver:
AssemblyScanner.FindValidatorsInAssemblyContaining<CleanupSiteValidator>()
.ForEach(match => Bind(match.InterfaceType).To(match.ValidatorType));
EDIT: I added InRequestScope()
to the resolver and the constructor for the validator now only fires once, but CreateInstance()
in NinjectValidatorFactory.cs
is still firing 32 times. I suppose this is...better, but should the factory be called this many times? It seems there is still something wrong.
AssemblyScanner.FindValidatorsInAssemblyContaining<CleanupSiteValidator>()
.ForEach(match => Bind(match.InterfaceType).To(match.ValidatorType).InRequestScope());