I have an MVC attribute set up like this:

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:

ActivationException: Error activating int No matching bindings are available, and the type is not self-bindable. Activation path:

2) Injection of dependency int into parameter directive of constructor of type ActivityFilter

1) Request for 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.

有帮助吗?

解决方案

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.)

Once I changed this it worked fine.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top