Translating Ninject to ASP.NET MVC 6 DI

冷暖自知 提交于 2019-12-03 12:41:15

The AddTransient method has various overloads, one of which accepts a lambda expression:

services.AddTransient<IDocumentStore>(s => CreateDocumentStore());

However it seems you are using the Ninject InSingletonScope() modifier so this may be more appropriate:

services.AddSingleton<IEmailSender>(s => CreateDocumentStore());

Additional note: There is some pre-release documentation available (of course, it's not complete and may be incorrect but may help)

Also you could continue use Ninject by adding Microsoft.Framework.DependencyInjection.Ninject to your project and then configure it with following code:

public IServiceProvider ConfigureServices(Microsoft.Framework.DependencyInjection.IServiceCollection services)
{
    var kernel = CreateMyKernel();
    kernel.Populate(services); // Wire up configured services and Ninject kernel with Microsoft tool
    return kernel.Get<IServiceProvider>();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!