简体   繁体   English

Ninject无法将枚举属性绑定到过滤器

[英]Ninject can't bind an enum property to a filter

I have an MVC attribute set up like this: 我有这样设置的MVC属性:

public class Activity : FilterAttribute
{
    public string[] Tags { get; set; }
    public Directive Directive { get; set; }
}

and a corresponding filter class 和相应的过滤器类

public class ActivityFilter : IActionFilter
{
    public ActivityFilter(IService service, IEnumerable<string> tags, Directive directive)
    {
       ...
    }
}

My bindings for the attribute to filter injection look like this: 我对用于过滤注入的属性的绑定如下所示:

this.BindFilter<ActivityFilter>(FilterScope.Action, 0)
  .WhenActionMethodHas<Activity>()
  .WithPropertyValueFromActionAttribute<Activity>("tags", a => a.Tags)
  .WithPropertyValueFromActionAttribute<Activity>("directive", a => a.Directive);

When I try to debug my web site, I get an error from Ninject stating that it can't activate the ActivityFilter because no matching bindings are available for int, specifically: 当我尝试调试网站时,我从Ninject收到一条错误消息,指出它无法激活ActivityFilter,因为没有匹配的绑定可用于int,特别是:

ActivationException: Error activating int No matching bindings are available, and the type is not self-bindable. ActivationException:激活int时出错没有匹配的绑定可用,并且该类型不可自绑定。 Activation path: 激活路径:

2) Injection of dependency int into parameter directive of constructor of type ActivityFilter 2)将依赖项int注入到ActivityFilter类型的构造函数的参数指令中

1) Request for ActivityFilter 1)请求ActivityFilter

It seems like Ninject is misinterpreting the enumeration parameter for an integer, but I'm at a loss as to how to resolve this problem. 似乎Ninject误解了整数的枚举参数,但我对如何解决此问题感到困惑。

The problem ended up being the method I was using for binding: 问题最终是我用于绑定的方法:

this.BindFilter<ActivityFilter>(FilterScope.Action, 0)
   .WhenActionMethodHas<Activity>()
   .WithPropertyValueFromActionAttribute<Activity>("tags", a => a.Tags)
   .WithPropertyValueFromActionAttribute<Activity>("directive", a => a.Directive);

I should have used WithConstructorArgumentFromActionAttribute since I was trying to inject into a constructor argument (I mistakenly thought that the Constructor/Property referred to the source of the value, not the destination .) 自从试图注入构造函数参数以来,我应该使用WithConstructorArgumentFromActionAttribute (我错误地认为Constructor / Property指的是值的来源 ,而不是目的地 。)

Once I changed this it worked fine. 一旦我更改了它,就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM