Closed sandord closed 3 years ago
There's a couple workarounds for scoped validators. Given that this library was based off of the Swashbuckle version, have you looked into some of it's workarounds? https://github.com/micro-elements/MicroElements.Swashbuckle.FluentValidation#common-problems-and-workarounds
I haven't had the time to port the HttpContextServiceProviderValidatorFactory yet, but this should solve the problem for HttpContext scopes. The implementation should be usable with this library as is. https://github.com/micro-elements/MicroElements.Swashbuckle.FluentValidation/blob/master/src/MicroElements.Swashbuckle.FluentValidation/HttpContextServiceProviderValidatorFactory.cs
From the MicroElements.Swashbuckle.FluentValidation examples.
`
// HttpContextServiceProviderValidatorFactory requires access to HttpContext
services.AddHttpContextAccessor();
services
.AddControllers()
// Adds fluent validators to Asp.net
.AddFluentValidation(c =>
{
c.RegisterValidatorsFromAssemblyContaining<Startup>();
// Optionally set validator factory if you have problems with scope resolve inside validators.
c.ValidatorFactoryType = typeof(HttpContextServiceProviderValidatorFactory);
})`
The scoped http context provided has been added in the last release.
Sorry for never responding to your reply. I appreciate the effort though and I'm happy to see that support has been added.
When I register the validators using the
AddValidatorsFromAssemblyContaining
service collection extension method, providing the scoped service lifetime (ServiceLifetime.Scoped
), I get a lot of instances of the following exception:Because of this, the validators are not processed.
I think that by creating a service scope and subsequently resolving services from the scope, this problem can be mitigated.