我想IValidatableObject.Validate()
在处理请求之前根据存储库检查模型的某些方面。但是,下面的配置_dalForValidation
永远不会设置在 onModels.App
中,换句话说,总是调用默认的空构造函数。
private static void ConfigureAutofac()
{
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<DataAccessFacade>().As<IDataAccess>().InstancePerApiRequest();
builder.RegisterType<Models.App>();
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
App
有 2 个构造函数:
public App(IDataAccess dalForValidation)
{
_dalForValidation = dalForValidation;
}
public App() {}
为了完整起见,这是我尝试访问它的地方,得到一个空引用异常:
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var existingApps = _dalForValidation.FindApps().Convert<DB.App,App>();
if (!ValidateProxyMappings(existingApps))
yield return new ValidationResult("Invalid proxy mapping");
}
也许依赖解析器没有用于模型,或者我在这里还缺少什么?