我一直试图让Ninject.Extensions.Conventions (尼尼姆3+)工作,但没有运气。我把它归结为一个找到的示例控制台应用程序,我甚至无法实现它。我现在拥有的是:
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses()
.BindAllInterfaces());
var output = kernel.Get<IConsoleOutput>();
output.HelloWorld();
var service = kernel.Get<Service>();
service.OutputToConsole();
Console.ReadLine();
}
public interface IConsoleOutput
{
void HelloWorld();
}
public class ConsoleOutput : IConsoleOutput
{
public void HelloWorld()
{
Console.WriteLine("Hello world!");
}
}
public class Service
{
private readonly IConsoleOutput _output;
public Service(IConsoleOutput output)
{
_output = output;
}
public void OutputToConsole()
{
_output.HelloWorld();
}
}
}我还尝试了各种组合的FromAssembliesMatching,SelectAllTypes,BindDefaultInterfaces,等。一切抛出错误激活。没有匹配的绑定可用,并且类型是不可自绑定的.
只是为了精神健康,如果我做了一个手动绑定:
kernel.Bind<IConsoleOutput>().To<ConsoleOutput>();一切都很好。所以很明显我只是遗漏了一些东西。
发布于 2013-09-18 14:33:01
正如萨姆所建议的,这是由那些不公开的类型造成的。它们是非公共“计划”类的内部类型。
使程序公开或添加.IncludingNonPublicTypes()
kernel.Bind(x => x
.FromThisAssembly()
.IncludingNonPublicTypes()
.SelectAllClasses()
.BindAllInterfaces());(我已经证实这两种方法都有效,而您的代码不起作用)。
注意:在Ninject的旧版本中,这种方法被称为IncludeNonePublicTypes (None vs Non)。
https://stackoverflow.com/questions/18793095
复制相似问题