本文介绍了Ninject 2.2多个绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新ASP.NET MVC 3应用Ninject 2.2。

previously我有以下接口来实现我的主要应用程序绑定:

<$p$p><$c$c>Bind(typeof(IMyInterface<>)).To(typeof(MyImplementation<>)).InRequestScope();

另外,我在这是一个由我的主要应用程序加载不同的装配如下:

  VAR ARG =新ConstructorArgument(信息,东西);
Bind<IMyInterface<MyClass>>().To<MyImplementation<BlogComment>>().WithParameter(arg);

这pviously工作的罚款$ P $和更具体的实施(带有参数)的识别。然而,当我升级到2.2 Ninject,我收到以下错误:

 错误激活IMyInterface的{} MyClass的
多个匹配的绑定可用。
激活路径:
 2)依赖IMyInterface的{} MyClass的注射入式SomeOtherClass的构造函数参数myParam
 1)请给IMyInterface的建议:
 1)确保您已经定义了IMyInterface的{} MyClass的一次绑定。

2.0是为了2.2什么变化,是造成这一点,有一个变通?


解决方案

Ninject 2.2确保只有一个匹配绑定解决实例时存在。 2.0返回的第一个匹配的绑定忽视了还有其他的实例。但如果要求仅一个具有多个绑定反映不好的配置,并且可导致很难检测意外行为

但我看应该有否决更具体的人开放通用绑定的可能性。我肯定会看看它,它要么被添加到一个修正版或下一个主要版本。

I recently updated ASP.NET MVC 3 app to Ninject 2.2.

Previously I had the following interface to implementation binding in my main app:

Bind(typeof(IMyInterface<>)).To(typeof(MyImplementation<>)).InRequestScope();

In addition, I had the following in a different assembly that was being loaded by my main app:

var arg = new ConstructorArgument("info", "something");
Bind<IMyInterface<MyClass>>().To<MyImplementation<BlogComment>>().WithParameter(arg);

This worked fine previously and the more specific implementation (the one with the argument) was being recognized. However, when I upgraded to Ninject 2.2, I received the following error:

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.

What change was made from 2.0 to 2.2 that is causing this and is there a work around?

解决方案

Ninject 2.2 ensures that only one matching bindings exists when resolving instances. 2.0 returned an instance of the first matching binding ignoring that there are others. But having multiple bindings if only one is requested reflects a bad configuration and can lead to hard to detect unintended behaviors.

But I see that there should be the possibility to overrule open generic bindings with more specific ones. I'll definitely look into it and it will either be added to a bugfix release or the next major release.

这篇关于Ninject 2.2多个绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:17