DI is for objects that have complex dependencies. Controllers, services, adapters, and repositories are all examples of objects that might be added to DI. In order to implement Dependency Injection by constructor in ASP.NET core. We have to use DI Containers (Autofac, Ninject, StructureMap). I was working on side project in ASP.NET core, came across this particular issue relevant to DI.
An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'Tweet.BAL.INewsData' while attempting to activate 'Tweet.Controllers.TweetController'.
Good news is that DI has been already part of Core framework, You just have to register your dependencies in startup.cs file under ConfigureService method.
services.AddTransient<IFoo, Foo>(); services.AddSingleton<IBar, Bar>(); services.AddScoped<IHello, Hello>();
The first generic type represents the type (typically an interface) that will be requested from the container. The second generic type represents the concrete type that will be instantiated by the container and used to fulfill such requests.
ASP.NET services can be configured with the following lifetimes:
Transient objects are always different; a new instance is provided to every controller and every service.
Scoped objects are the same within a request, but different across different requests
Singleton objects are the same for every object and every request (regardless of whether an instance is provided in