尝试将数据注入FluentValidation验证器:
public class MyFormValidator : AbstractValidator{ private readonly IQueryable _users; public MyFormValidator(IQueryable users) { _users = users; ... } }
我的验证工厂:
public class DependencyResolverValidatorFactory : ValidatorFactoryBase { private readonly IContainer container; public DependencyResolverValidatorFactory(IContainer container) { this.container = container; } public override IValidator CreateInstance(Type validatorType) { return container.ResolveOptionalKeyed(validatorType); } }
我的Autofac配置器:
public class AutofacConfigurator { public static void Configure() { var builder = new ContainerBuilder(); ... builder.RegisterType() .Keyed (typeof(IValidator )) .As () // 2nd parameter returns IQueryable .WithParameter("users", new SqlRepository (dataContext)) .InstancePerRequest(); builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource()); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // Register the validator factory with FluentValidation, and register // FluentValidation as the model validator provider for the MVC framework. // see http://www.jerriepelser.com/blog/using-fluent-validation-with-asp-net-mvc-part-3-adding-dependency-injection var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider( new DependencyResolverValidatorFactory(container)); DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false; ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider); } }
获得以下异常:
从请求实例的作用域中看不到具有匹配"AutofacWebRequest"的标记的作用域.这通常表示SingleInstance()组件(或类似场景)正在请求注册为每HTTP请求的组件.在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖项,从不从容器本身请求.
我有其他验证器,其中大部分都不需要注入数据.
这对我来说是很大的新理由(在Autofac和FluentValidation中),我仍在努力理解我在这里做的事情.我怀疑我只是错误地注册我的类型.我如何解决此问题并正确注册我的类型?
(如果这与已经提出的其他问题过于相似,我表示道歉.)
我对FluentValidation缺乏经验,但我怀疑这是你问题的原因,所以无论如何我都会向前推进.
您获得的例外意味着Autofac无法将您的服务解析为"每个请求的实例".关于Autofac 文档页面上的含义,有很多文档.总而言之,这意味着Autofac将尝试从为发送到Web服务器的每个请求自动创建的生命周期范围中解析服务.当您注册某些内容.InstancePerRequestScope()
但然后尝试在该范围之外解析该服务时,您会DependencyResolutionException
看到.
所以我们已经确定你MyFormValidator
的"请求"范围没有得到解决.为什么?
DependencyResolverValidatorFactory
您编写的自定义IContainer
采用Autofac构建的实际,并从中解析.这是一种特殊类型ILifetimeScope
的"根范围".没有与此直接关联的请求生命周期范围,因此您将获得异常.您需要ILifetimeScope
从"请求"范围或请求范围中包含的子范围开始解析.
Autofac/MVC集成已经自动托管请求范围(在AutofacDependencyResolver
查看源代码中),但您的自定义DependencyResolverValidatorFactory
不能从中解析.如果你想这样做,我想你可以修改你DependencyResolverValidatorFactory
的接受AutofacDependencyResolver
实例,然后用它来解决.
它看起来像这样:
public class DependencyResolverValidatorFactory : ValidatorFactoryBase { private readonly AutofacDependencyResolver resolver; public DependencyResolverValidatorFactory(AutofacDependencyResolver resolver) { this.resolver = resolver; } public override IValidator CreateInstance(Type validatorType) { return resolver.RequestLiftimeScope.ResolveOptionalKeyed(validatorType); } }
注意RequestLifetimeScope
卡在那里.然后在您的.Configure()
方法中使用创建它
var resolver = new AutofacDependencyResolver(container); DependencyResolver.SetResolver(resolver); var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider( new DependencyResolverValidatorFactory(resolver));
这应该摆脱异常,假设这个工厂确实有一个请求在创建IValidator
s的实例时工作.如果没有,您可能需要使用默认行为(.InstancePerDependency()
每次请求时创建新实例)或单例(.SingleInstance()
)进行注册,具体取决于验证者可以或应该如何共享.
祝好运.