NInject实例化多个服务

时间:2016-09-14 07:32:01

标签: ninject decorator

我按如下方式配置了装饰器

Bind<IEmailService>().To<EmailService>().WhenInjectedInto<LoggerEmailService>();
Bind<IEmailService>().To<LoggerEmailService>().WhenInjectedInto<ExceptionHandlerEmailService>();
Bind<IEmailService>().To<ExceptionHandlerEmailService>();

当我在服务实现的ctor中放置一个断点时,比如ExceptionHandlerEmailService或者只是EmailService,我发现该服务被多次调用。我错过了什么?

1 个答案:

答案 0 :(得分:0)

.InSingletonScope()就是所需要的。

Bind<IEmailService>().To<ExceptionHandlerEmailService>().InSingletonScope();
Bind<IEmailService>().To<LoggerEmailService>().WhenInjectedInto<ExceptionHandlerEmailService>();
Bind<IEmailService>().To<EmailService>().WhenInjectedInto<LoggerEmailService>();