我最近将ASP.NET MVC 3应用程序更新为Ninject 2.2。
以前我在主应用程序中有实现绑定的以下界面:
Bind(typeof(IMyInterface<>)).To(typeof(MyImplementation<>)).InRequestScope();
此外,我在主应用程序加载的另一个程序集中有以下内容:
var arg = new ConstructorArgument("info", "something");
Bind<IMyInterface<MyClass>>().To<MyImplementation<BlogComment>>().WithParameter(arg);
此前工作正常,并且正在识别更具体的实现(带有参数的实现)。但是,当我升级到Ninject 2.2时,我收到以下错误:
Error activating IMyInterface{MyClass}
More than one matching bindings are available.
Activation path:
2) Injection of dependency IMyInterface{MyClass} into parameter myParam of constructor of type SomeOtherClass
1) Request for IMyInterface
Suggestions:
1) Ensure that you have defined a binding for IMyInterface{MyClass} only once.
导致这种情况从2.0到2.2发生了什么变化,是否有解决方法?
答案 0 :(得分:5)
Ninject 2.2确保在解析实例时只存在一个匹配的绑定。 2.0返回了第一个匹配绑定的实例,忽略了其他绑定。但如果只有一个请求,则拥有多个绑定会反映出错误的配置,并且可能导致难以检测到意外行为。
但是我发现应该有可能用更具体的方法否决开放的通用绑定。我肯定会调查它,它将被添加到错误修复版本或下一个主要版本。