2012-11-27 58 views
1

另一种电报式问题。首先,我NinjectModule:跨项目解决互操作性依赖关系

using Ninject.Modules; 
using Microsoft.Office.Interop.Word; 

namespace NinjectSample.Util.Injection 
{ 
    public class SampleModule : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<_Application>().ToMethod(ctx => GetApp()); 
     } 

     public _Application GetApp() 
     { 
      return new Application(); 
     } 
    } 
} 

第一件事情(工作!):

 IKernel kernel = new StandardKernel(new SampleModule()); 
     var foo = kernel.Get<_Application>(); 

改变这

 IKernel kernel = new StandardKernel(new SampleModule()); 
     var foo = kernel.Get<BusinessClass>(); 

BusinessClass在另一个大会,代码定义:

namespace BusinessClassLibrary 
{ 
    public class BusinessClass 
    { 
     private _Application _app; 

     [Inject] 
     public BusinessClass(_Application application) 
     { 
      _app = application; 
     } 
    } 
} 

This will Result在:

Error activating _Application 
No matching bindings are available, and the type is not self-bindable. 
Activation path: 
    2) Injection of dependency _Application into parameter application of constructor of type BusinessClass 
    1) Request for BusinessClass 

Suggestions: 
    1) Ensure that you have defined a binding for _Application. 
    2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 
    3) Ensure you have not accidentally created more than one kernel. 
    4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 
    5) If you are using automatic module loading, ensure the search path and filters are correct. 

我不是太深入的互操作,但我的.Net有基本的了解让我遇到下列绊倒:

  • Application是一个接口,就像它的基础_Application。不过可以调用它的构造函数。为什么?

  • 我告诉Ninject使用一个工厂方法,它似乎在依赖位于定义了内核的程序集中时工作。为什么它尝试通过自绑定来解析依赖关系,当它位于另一个部件?

回答

1

应用是一个接口,就像它的基地_Application。 尽管如此,可以调用它的构造函数。为什么?

这是一个编译器技巧;有关正在发生的事情的信息,请参阅Creating instance of interface in C#。当你在代码中遇到它时,它确实看起来很奇怪。不幸的是,目前无法帮助您解决问题的实际Ninject部分。

+0

感谢您指出CoClass业务! –