Dependency injection 使用Ninject 3.0.0基于约定的依赖项注入

Dependency injection 使用Ninject 3.0.0基于约定的依赖项注入,dependency-injection,ninject,ninject-extensions,Dependency Injection,Ninject,Ninject Extensions,我的解决方案中有两个项目。。。域项目和MVC3 web项目(例如MyApp.domain和MyApp.web)。以前,当使用Ninject.Extensions.Conventions时。2,我能够在NinjectMVC3.cs文件中使用以下语句,并且正确地注入了整个解决方案(web和域)中所需的依赖项(例如,IFoo自动绑定到Foo) 有没有线索告诉我如何使用新的Ninject版本配置基于约定的绑定?我假设这与指定程序集有关,但我尝试使用了assembliesmatching(“*”)中的,然

我的解决方案中有两个项目。。。域项目和MVC3 web项目(例如MyApp.domain和MyApp.web)。以前,当使用Ninject.Extensions.Conventions时。2,我能够在NinjectMVC3.cs文件中使用以下语句,并且正确地注入了整个解决方案(web和域)中所需的依赖项(例如,IFoo自动绑定到Foo)

有没有线索告诉我如何使用新的Ninject版本配置基于约定的绑定?我假设这与指定程序集有关,但我尝试使用了assembliesmatching(“*”)中的
,然后一切都失败了

--编辑以在RegisterServices方法中显示我的现有代码:-

private static void RegisterServices(IKernel kernel)
{
  // This code used to work with v.2 of Ninject.Extensions.Conventions
  // kernel.Scan(x =>
  // {
  //   x.FromAssembliesMatching("*");
  //   x.BindWith<DefaultBindingGenerator>();
  // });

  // This is the new v3 code that automatically injects dependencies but only for interfaces in MyApp.Web, not MyApp.Domain
  kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().BindToAllInterfaces()); 

  // I tried this code, but it throws "Error activating IDependencyResolver" at "bootstrapper.Initialize(CreateKernel)"
  // kernel.Bind(x => x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath).SelectAllClasses().BindToAllInterfaces());

  // These are dependencies in MyApp.Web that ARE being bound properly by the current configuration
  // kernel.Bind<IMemberQueries>().To<MemberQueries>();
  // kernel.Bind<IGrantApplicationQueries>().To<GrantApplicationQueries>();
  // kernel.Bind<IMailController>().To<MailController>();

  // These are dependencies in MyApp.Domain that ARE NOT being bound properly by the current configuration, so I have to declare them manually 
  // They used to be injected automatically with version 2 of the conventions extention
  kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
  kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
  kernel.Bind<IMemberServices>().To<MemberServices>();
  kernel.Bind<IGrantApplicationServices>().To<GrantApplicationServices>();

  // These are dependencies that SHOULD NOT be bound by convention as they require a different scope or have unique naming
  kernel.Bind(typeof(EfDbContext)).ToSelf().InRequestScope();
  kernel.Bind<IConfigurationProvider>().To<WebConfigConfigurationProvider>().InSingletonScope();
  kernel.Bind<IAuthorizationProvider>().To<MyAppAuthorizationProvider>();
  kernel.Bind<IPrincipal>().ToMethod(ctx => HttpContext.Current.User).InRequestScope();
  kernel.Bind<IGrantApplicationDocumentServices>().To<MySpecialNameGrantAplicationDocumentServices>().InRequestScope();
}
私有静态无效注册服务(IKernel内核)
{
//此代码用于处理Ninject.Extensions.Conventions的v.2
//kernel.Scan(x=>
// {
//x.FromAssembliesMatching(“*”);
//x.BindWith();
// });
//这是新的v3代码,自动注入依赖项,但仅适用于MyApp.Web中的接口,而不是MyApp.Domain中的接口
Bind(x=>x.FromThisAssembly().SelectAllClasses().BindToAllInterfaces());
//我尝试了这段代码,但它在“bootstrapper.Initialize(CreateKernel)”处抛出“错误激活IDependencyResolver”
//Bind(x=>x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath)。选择AllClasses().BindToAllInterfaces());
//这些是MyApp.Web中的依赖项,它们正被当前配置正确绑定
//kernel.Bind().To();
//kernel.Bind().To();
//kernel.Bind().To();
//这些是MyApp.Domain中的依赖项,它们没有被当前配置正确绑定,因此我必须手动声明它们
//它们过去是自动注入conventions扩展的版本2的
Bind(typeof(IRepository)).To(typeof(Repository)).InRequestScope();
kernel.Bind().To().InRequestScope();
kernel.Bind().To();
kernel.Bind().To();
//这些依赖项不应受约定的约束,因为它们需要不同的范围或具有唯一的命名
Bind(typeof(EfDbContext)).ToSelf().InRequestScope();
kernel.Bind().To().InSingletonScope();
kernel.Bind().To();
kernel.Bind().ToMethod(ctx=>HttpContext.Current.User).InRequestScope();
kernel.Bind().To().InRequestScope();
}
等价物是:

kernel.Bind(x => x
    .FromAssembliesMatching("*")
    .SelectAllClasses()
    .BindDefaultInterface());

你看着我。如果没有,你能告诉我哪一点对你来说没有意义吗?鲁本,我已经看过维基了。没有意义的是,使用旧代码
x.FromAssembliesMatching(“*”)
工作并绑定了MyApp.Web和MyApp.Domain中的所有接口。但是,在新的3.0.0中,这种语法不起作用。我找到的最接近的是
x.fromthissassembly()
,但它只绑定MyApp.Web中的接口(因为这是注入发生的地方)。它不会自动绑定MyApp.Domain中的接口。查看代码,您始终可以自己获取程序集(通过AppDomain的程序集列表或某些opther机制)并显式提供该列表。除此之外,我建议你只需阅读源代码——它并不算大。查看该算法,它使用AppDomain relativesearchpoath和basepath的组合。作为排除任何问题的快速方法,您可以从AssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath)
执行
,这将产生
感谢您对Ruben的所有帮助。不幸的是,我尝试过这样做,但现在我遇到一个异常,说明“激活IDependencyResolver时出错。有多个匹配的绑定可用。激活路径:1)请求IDependencyResolver。建议:1)确保仅为IDependencyResolver定义了一次绑定。”。我希望新版本能像第二版一样工作。谢谢Remo。我想我是个白痴。这是我尝试的第一件事,但我使用了bindtoDefaultiInterface(复数)方法,但失败了。当我把它改为单数法时,它起了作用。很抱歉所有的帖子,但我非常感谢你的帮助。你所有的项目都做得很好!
private static void RegisterServices(IKernel kernel)
{
  // This code used to work with v.2 of Ninject.Extensions.Conventions
  // kernel.Scan(x =>
  // {
  //   x.FromAssembliesMatching("*");
  //   x.BindWith<DefaultBindingGenerator>();
  // });

  // This is the new v3 code that automatically injects dependencies but only for interfaces in MyApp.Web, not MyApp.Domain
  kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().BindToAllInterfaces()); 

  // I tried this code, but it throws "Error activating IDependencyResolver" at "bootstrapper.Initialize(CreateKernel)"
  // kernel.Bind(x => x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath).SelectAllClasses().BindToAllInterfaces());

  // These are dependencies in MyApp.Web that ARE being bound properly by the current configuration
  // kernel.Bind<IMemberQueries>().To<MemberQueries>();
  // kernel.Bind<IGrantApplicationQueries>().To<GrantApplicationQueries>();
  // kernel.Bind<IMailController>().To<MailController>();

  // These are dependencies in MyApp.Domain that ARE NOT being bound properly by the current configuration, so I have to declare them manually 
  // They used to be injected automatically with version 2 of the conventions extention
  kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
  kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
  kernel.Bind<IMemberServices>().To<MemberServices>();
  kernel.Bind<IGrantApplicationServices>().To<GrantApplicationServices>();

  // These are dependencies that SHOULD NOT be bound by convention as they require a different scope or have unique naming
  kernel.Bind(typeof(EfDbContext)).ToSelf().InRequestScope();
  kernel.Bind<IConfigurationProvider>().To<WebConfigConfigurationProvider>().InSingletonScope();
  kernel.Bind<IAuthorizationProvider>().To<MyAppAuthorizationProvider>();
  kernel.Bind<IPrincipal>().ToMethod(ctx => HttpContext.Current.User).InRequestScope();
  kernel.Bind<IGrantApplicationDocumentServices>().To<MySpecialNameGrantAplicationDocumentServices>().InRequestScope();
}
kernel.Bind(x => x
    .FromAssembliesMatching("*")
    .SelectAllClasses()
    .BindDefaultInterface());