ASP.NET Core includes that makes configured services available throughout an app. Services are added to the DI container with WebApplicationBuilder.Services.
ASP.NET Core provides a built-in service container, IServiceProvider, in which any can be registered.
Program.cs
usingMicrosoft.EntityFrameworkCore;usingRazorPagesMovie.Data;var builder =WebApplication.CreateBuilder(args);var connectionStr =builder.Configuration.GetConnectionString("DefaultConnection");// Add services to the container.builder.Services.AddRazorPages();builder.Services.AddControllersWithViews();builder.Services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer(connectionString));builder.Services.AddDatabaseDeveloperPageExceptionFilter();builder.Services.AddDefaultIdentity<IdentityUser>(options =>options.SignIn.RequireConfirmedAccount=true) .AddEntityFrameworkStores<ApplicationDbContext>();var app =builder.Build();
Services are typically resolved from DI using constructor injection. The DI framework provides an instance of this service at runtime.
Injection of the service into the constructor of the class where it's used. The framework takes on the responsibility of creating an instance of the dependency and disposing of it when it's no longer needed.
Is typically an object that provides a service to other objects, such as the IMyDependency service.
Is not related to a web service, although the service may use a web service.
IServiceCollection - register services
IServiceProvider - resolve service instances
Service Lifetimes
Transient objects are always different.
Scoped objects are the same for a given request but differ across each new request.
Singleton objects are the same for every request.
The container calls Dispose() for the IDisposable types it creates. Services resolved from the container should never be disposed by the developer. If a type or factory is registered as a singleton, the container disposes the singleton automatically.
The service instances that aren't created by the service container, The framework doesn't dispose these services automatically. The developer is responsible for disposing the services.
High-level modules should not depend on low-level modules.
Abstraction should not depend upon details
Modules and Details should depend upon abstraction.
Benefits of Dependency Injection
promotes loose coupling of components
promotes logical abstraction of components
supports unit testing
How to find dependency?
Locate 'new' keyword usage
is the object a dependency?
apply dependency inversion
register the service
rinse and repeat
AddTransient
AddScoped
AddSingleton
instance of services are created each time they are requested
instance of services are created once per request
instance of services are created only once, the first time they were requested
Understanding Service Lifetime
Built-in IoC container manages the lifetime of a registered service type. It automatically disposes a service instance based on the specified lifetime.
The built-in IoC container supports three kinds of lifetimes:
Singleton: IoC container will create and share a single instance of a service throughout the application's lifetime.
or, once for the lifetime of the application.
Transient: The IoC container will create a new instance of the specified service type every time you ask for it.
or, each time they are requested.
Scoped: IoC container will create an instance of the specified service type once per request and will be shared in a single request.
or, once per request
Once we register a service, the IoC container automatically performs constructor injection if a service type is included as a parameter in a constructor.
Sometimes we may only need dependency service type in a single action method. For this, use [FromServices] attribute with the service type parameter in the method.
It is not required to include dependency services in the constructor. We can access dependent services configured with built-in IoC container manually using RequestServices property of HttpContext