Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are services and why add them in ASP.NET Core?

I begin to learn ASP.NET Core, there, within the framework of the Web API template, there is a Startup class, in which the ConfigureServices() method is defined. Can someone explain in simple words what services he sets up, and why do they need them? Thank!

like image 512
Mihail Avatar asked Apr 25 '19 08:04

Mihail


People also ask

Can I add a service to an ASP NET Core website?

ASP.NET - Adding a Service to an ASP.NET Core Website [4 of 13] ASP.NET Core 101 ASP.NET uses a technique called "dependency injection" that's built right in. That lets us make classes called "services" that we can make available to any other classes in our website. Let's add a service now! Follow: Scott Hanselman, Leslie Richardson

What is ASP NET Core hosted service?

ASP.NET Core Hosted Service is the class where background logical task that implements the IHostedService Interface, the background tasks implemented as hosted services, and those executes on a timer. This hosted service is activated as scoped service, and it uses the Dependency Injection (DI).

What is a background task in ASP NET Core?

Background task that runs on a timer. Hosted service that activates a scoped service. The scoped service can use dependency injection (DI). Queued background tasks that run sequentially. The ASP.NET Core Worker Service template provides a starting point for writing long running service apps.

What is web services in ASP NET?

ASP.NET - Web Services. A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications.


3 Answers

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.

like image 60
poke Avatar answered Sep 28 '22 04:09

poke


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.

like image 40
Mark Redman Avatar answered Sep 28 '22 06:09

Mark Redman


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 the services.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

like image 28
Pranav Singh Avatar answered Sep 28 '22 06:09

Pranav Singh