Ninject 3.2 BindFilter constructor binding broken when using Ninject.Extension.Factory 3.2

StackOverflow https://stackoverflow.com/questions/22613586

質問

I have an MVC5, WebApi application that works fine if I don't include the Ninject.Extension.Factory 3.2.0.0 3/21/2014 build. Once I add that package the application has the following exception:

Error activating EntitleActionFilter using self-binding of EntitleActionFilter
Several constructors have the same priority. Please specify the constructor using ToConstructor syntax or add an Inject attribute.

Constructors:
EntitleActionFilter(List{NavigationItem} navigationItemsFunc{IEnumerable{string}} permissionFunc)
EntitleActionFilter(List{NavigationItem} navigationItemsFunc{IEnumerable{int}} permissionFuncInt)

I'm using the following Ninject packages (all Friday, 3/21/2014 builds):

  • Ninject 3.2.0.0
  • Ninject.MVC5 3.2.0.0
  • Ninject.Web.Common 3.2.0.0
  • Ninject.Web.Common.WebHost 3.2.0.0

Those two constructors are unique. The bindings look like:

Func<IEnumerable<int>> permissionFunc = () => Kernel.Get<INavigationPermissionsProvider>().GetPermissionsInt();

this.BindFilter<EntitleActionFilter>(FilterScope.Controller, 0)
    .WithConstructorArgument("navigationItems",
         context => context.Get<NavigationHelper>().GetNavigation().NavigationItems)
    .WithConstructorArgument("permissionFuncInt", permissionFunc);

I do use Ninject factories a lot and need this package. If I remove the Ninject.Extensions.Factory 3.2 package, everything works. This same code worked with the previous 3.0 version of Ninject and the factory extension.

役に立ちましたか?

解決

This is not related to the factory extension. The factory extension just adds bindings for all Func overloads which means both constructors can be satisfied completely.

With Ninject 3.0 any of the constructor is chosen if there were several with the same priority. There is no guarantee that always the same one is picked. Which one is picked is decided by the implementation of some .NET Framework functions that give no guarantee about ordering. In the worst case it means that the .NET framework version decides what constructor is picked.

Ninject 3.2 throws now an exception if there are several constructors with the same priority so that you have to configure which constructor is taken, so that always the correct one is picked.

If I were you I wouldn't use an Func as dependency but instead inject an instance of INavigationPermissionsProvider and call GetPermissionsInt()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top