对于所有类型,如何告诉ninject 3默认使用单例范围?

Dav*_* MZ 5 .net dependency-injection ninject inversion-of-control

我希望我的应用程序始终将服务用作单例,如何设置Ninject默认使用单例作用域.我使用约定来注册我的类型,我需要使用Bind <>方法吗?

Dan*_*ach 8

Hy,假设您继承的所有服务都IService可以编写以下内容

添加以下using语句

using Ninject.Extensions.Conventions;
Run Code Online (Sandbox Code Playgroud)

使用像这样的约定

kernel.Bind( x => x
.FromThisAssembly()
.SelectAllClasses().InheritedFrom<IService>()
.BindAllInterfaces()
.Configure(b => b.InSingletonScope()));
Run Code Online (Sandbox Code Playgroud)

您可能需要稍微调整一下以满足您的需求.