The Program.cs and Startup.cs files

Let's continue by examining the Program.cs file of a web API project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace SampleAPI
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[]
args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

The preceding code imports the Microsoft.AspNetCore.Hosting and Microsoft.Extensions.Hosting namespaces. They supply the necessary references for the initialization of a new IHostBuilder instance created in the CreateHostBuilder function. The CreateHostBuilder function executes the Host.CreateDefaultBuilder method, which initializes the web host of our APIs. Furthermore, we should note that the IHostBuilder instance returned by the CreateDefaultBuilder method refers to the Startup class of the project. The Main method invokes the CreateHostBuilder function and executes the Build and Run methods exposed by the IHostBuilder interface.

Let's examine the Startup class (defined in the Startup.cs file), which is used to configure the application stack:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace SampleAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}

public void Configure(IApplicationBuilder app,
IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

The Startup class initializes the IConfiguration attribute through dependency injection. The IConfiguration object represents a key/value object which contains configurations for the app. By default, the CreateDefaultBuilder method declared in the Program.cs file sets appsettings.json as the default configuration file.

The Startup class has two different methods, which behave in the following ways:

  • The ConfigureServices method configures services in our application using dependency injection. By default, it adds controllers by executing the .AddControllers extension method. In ASP.NET Core, the term services usually refer to any component or class that provides our application with features and functionalities. As we'll see in the next few chapters, ASP.NET Core frequently uses dependency injection to maintain a good design and loosely-coupled classes.
  • The Configure method is used to configure the application's middleware pipeline. It accepts two arguments: IApplicationBuilder and IWebHostEnvironment. The first contains all the pipelines for our app and exposes extension methods to build our app with middleware. We'll have a look at middleware in detail in Chapter 3, Working with the Middleware Pipeline. The IWebHostEvironment interface gives some information about the current hosting environment of the application, such as its type and its name. In a web API project, the Configure method executes a list of extensions methods. The most important are the UseRouting and UseEndpoints extension methods. The execution of the UseRouting method defines the point in the pipeline where routing decisions are taken. The UseEndpoints extension method defines the actual execution of the previously selected endpoint. In the case of a web API project, the only endpoints involved are the controllers. Therefore, the UseEndpoints method executes the MapControllers extension method to initialize the default routing convention for controller classes, provided in .NET Core.

It should be noted that the ASP.NET Core Startup class provides a high-level, code-first way to configure the dependencies of your application through dependency injection, which means that it only initializes what you need. Furthermore, .NET Core is strongly modularity-oriented; this is one of the reasons why it performs better than the .NET Framework.

Since all pipelines and dependencies are initialized in the aforementioned class, you know exactly where they can be changed. In large applications and services with a lot of different components, it is advisable to create custom extension methods that handle the initialization of specific parts of your app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.119.114