I have been using NInject for some time, and now I will be starting a project in Asp.net core. Seems NInject cannot be used with Asp.net Core. So now my question is does Asp.net core provide any di modules just as NInject or other di containers provide?
-
1You misinterpreted [this](https://stackoverflow.com/questions/32788637/continued-ninject-support-in-asp-net-mvc-6/32797105#32797105) answer. The answer clearly states that Ninject *can* be used. – Steven Nov 07 '16 at 10:16
4 Answers
ASP.NET Core doesn't provide any module support at all. It is designed to be a simple out-of-the-box container where 3rd parties IoC container can plug-in.
So there are no auto-registration, assembly scannings, decorators, interceptors or modules. If you want them, you need to use 3rd party frameworks (AutoFac, StructureMap etc.).
3rd party libraries do register using Registration methods, which are called like services.AddXxx()
in ConfigureServices
method.
public static class MyLibraryServiceCollectionExtensions
{
public static IServiceCollection AddMyLibrary(this IServiceCollection services)
{
services.TryAddScoped<IMyService,MyService>();
return services;
}
}
It is preferred way to register libraries (cause it doesn't depend on any IoC container except for the built-in IServiceCollection
, which 3rd party containers use too when plugged in ASP.NET Core), for stuff like "business logic library" there isn't really such a thing (where modules were useful before).

- 61,549
- 15
- 193
- 205
Just to add to the other good answers, you can use e.g. SimpleInjector. It's a brilliant DI-container that supports .Net Core (and ASP.Net Core as well). See details of how to use it with .Net Core here.

- 27,817
- 27
- 121
- 207
Asp.Net Core provides out of the box DI framework, it allows you to add an object for each resolve request, per Http Request or a singleton.
All of this is done in the ConfigureServices
method
ex:
public void ConfigureServices(IServiceCollection services)
{
//Your configuration
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
You can read more about it here
https://docs.asp.net/en/latest/fundamentals/dependency-injection.html

- 4,336
- 2
- 14
- 19
-
4It doesn't really answer the OPs question if there is a module support, where the modules is defined in each class library and does it registrations there and the main app only loads the module class during bootstrapping – Tseng Nov 07 '16 at 08:14
-
How come it is not answering the question? the question is about asp.net core support for a DI, and the answer is it has a ready made one, so what would be the answer from your POV? and why should we suggest another DI like ninject if it is already there OOB? – Haitham Shaddad Nov 07 '16 at 08:16
-
2No, the question is not about ASP.NET Core Support for DI, the OP knows it is there. He specifically asked about modules. Modules are classes placed in the library which perform registrations in contrast to performing the registrations in the mail application. You can read here https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel about the modules in Ninject. Other IoC have it too (CastleWindsor, Autofac, Unity Container). But ASP.NET Core's DI doesn't have that for the reasons I pointed in my answer – Tseng Nov 07 '16 at 08:19