What are services and why add them in ASP.NET Core?
ConfigureServices has one parameter, of type IServiceCollection. IServiceCollection, this is a DI (Dependency Injection) container. Adding services to this container will make them available for dependency injection. That means we can inject those services anywhere in our application. ConfigureServices is primarily for DI and setting up various library setup included for your project.
The
ConfigureServices
method is:
- Optional.
- Called by the host before the Configure method to configure the app's services.
- Where configuration options are set by convention.
The typical pattern is to call all the
Add{Service}
methods and then call all of theservices.Configure{Service}
methods. For example, see [Configure Identity services][1].The host may configure some services before Startup methods are called. For more information, see The host.
For features that require substantial setup, there are Add{Service} extension methods on
IServiceCollection
. A typical ASP.NET Core app registers services for Entity Framework, Identity, and MVC:public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<IdentityUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); }
Adding services to the service container makes them available within the app and in the Configure method. The services are resolved via dependency injection or from
ApplicationServices
.
Refer: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.2#the-configureservices-method
ASP.NET Core uses dependency injection as a fundamental feature to manage dependencies throughout the framework. In order for the dependency injection framework to know how to resolve dependencies, these dependencies or “services” need to be configured first.
ASP.NET Core does this already for the very core services when you create the web host in your Program.cs
but as you enable more features in your web application, you will need to add additional services to the application to opt into functionality.
For example services.AddMvc()
adds the services required to enable the MVC functionality and middleware in the application. Or services.AddAuthentication()
adds the services that are required to enable authentication in your application.
Since these functionalities are opt-in based and not enabled by default, the author of an application needs a way to control this. That is why the ConfigureServices
method is there: Here, you can add the services you want to enable the functionality.
In addition, you can also use this to add your own services so that you can make use of dependency injection within the application as well; for example to resolve your own services within a controller.
Dependency injection is actually a rather complex topic, so I would suggest you to take a look at the documentation on dependency injection to see how it works and what you can do with it.