我在我的項目中使用EasyNetQ庫,並且我想將Ninject用作EasyNetQ組件的IoC容器。使用Ninject作爲IoC for EasyNetQ
public class LogNothingLogger : IEasyNetQLogger
{
...
}
然後在我的主要功能使用Ninject擴展:
我,以便從EasyNetQ登錄anythong創建自定義記錄器
public class Program
{
static void Main(string[] args)
{
// Container creation and custom logger registration
IKernel cointainer = new StandardKernel();
cointainer.Bind<IEasyNetQLogger>().To<LogNothingLogger>();
// Register Ninject as IoC Container for EasyNetQ
cointainer.RegisterAsEasyNetQContainerFactory();
// Create bus
using (IBus bus = RabbitHutch.CreateBus("host=localhost"))
{
// Do something with bus...
}
}
}
,但我得到以下異常:
Ninject.ActivationException was unhandled
More than one matching bindings are available.
Matching bindings:
1) binding from IEasyNetQLogger to LogNothingLogger
2) binding from IEasyNetQLogger to method
Activation path:
2) Injection of dependency IEasyNetQLogger into parameter logger of constructor of type RabbitBus
1) Request for RabbitBus
Suggestions:
1) Ensure that you have defined a binding for IEasyNetQLogger only once.
[...]
我是否以錯誤的方式使用這個軟件包?有沒有解決方法?
謝謝!
嗨BatteryBackupUnit, 我試圖重新綁定,但我仍然得到錯誤。如果我不綁定任何東西到記錄器EasyNetQ繼續使用默認的。 這就像語句'CreateBus'在ninject之後仍然綁定它自己的組件。在Estension測試中有一個奇怪的評論[鏈接](https://github.com/mikehadlow/EasyNetQ/blob/master/Source/EasyNetQ.DI.Tests/NinjectAdapterTests.cs): 'Ninject不允許更多比服務的一個註冊,它引發一個例外' 這似乎仍然是一個公開的問題。 – Ganto
在這種情況下,EasyNetQ在您調用重新綁定後創建一個綁定。您需要弄清EasyNetQ日誌應該如何配置。你需要找出是誰在呼叫'NinjectAdapter.Register(Func <...'。也許還有一些你應該配置EasyNetQ的方法 - 不是ninject-用於記錄。嘗試刪除您的綁定,並使用'IResolutionRoot.Get ' - 看看它返回什麼? –
BatteryBackupUnit