.Net 6 + WebApi Basics 5 - Entry Point and Configuration File
2023-01-07 23:24:27  .Net  >> .Net6  >> WebApi

Entry Point and Configuration File

Entry Point File for .Net 6

In .Net framework, every program should have a main() in Program.cs as the entry point file. In .Net 6, Microsoft introduced the new concepts of C# 9.0, Top-level statement, so that there is no main() any more.

Even the main() is no longer shown explicitly, it can be still shown when decompiling. So you can treat it as the normal main() exists, which limits only 1 entry point file per project can use the top-level statement.

Structure of Program.cs

Let’s watch the code line by line.


1
var builder = WebApplication.CreateBuilder(args);

builder will be the object to access point to configure services in the HTTP pipeline and routes.


1
2
3
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services is used to configure the services.

This is actually the concept of the IOC container, which uses dependency injection to achieve the following functions.

Dependency Injection

Simply put, dependency injection is not as complex as its name.

Whenever we call a class, we need to use new.

1
var x = new object();

The IOC container is to help the project execute the above new operation in advance. This operation is called dependency injection of service.

For example,

builder.Services.AddControllers();

AddControllers() as its name is to inject service of controllers in advance. During this process, IOC is equivalent to calling the constructor in WeatherForecastController.cs as follows.

1
2
3
4
5
6
7
8
9
private readonly ILogger<WeatherForecastController> _logger;
/// <summary>
/// This is Constructor
/// </summary>
/// <param name="logger"></param>
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

Here we can see that there is parameter in the constructor, so when we call, IOC automatically passes in an ILogger parameter.

If it is another type of parameter, IOC will also be responsible for passing in other parameters.


Configuration File

We can get the configurations of project in appsettings.json.

1
2
3
4
5
6
7
8
9
10
//appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

Use configurations in Program.cs

Configure the settings of appsettings.json by following operations.

1
2
3
4
5
6
7
8
//Level 1
Configuration["xxxx"];
//Level 2
Configuration["xxxx:yyyy"];
//Convert type
Configuration.GetValue("xxx");
//Get and serialize to a certain type
Configuration.GetSection("xxx").Get<"TYPE">();

For example,

Modify the appsettings.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Test": "ABC",
"Test2": "123",
"UserSetting": {
"Name": "Steven",
"Age": 18
}
}

Add following codes in Program.cs to use configurations.

1
2
3
var x1 = builder.Configuration["Test"];
var x2 = builder.Configuration["Logging:LogLevel:Default"];
var x3 = builder.Configuration.GetValue<int>("Test2");

Result:
x1x2x3

Note: x3 is converted from string to int.

1
var x4 = builder.Configuration.GetSection("UserSetting").Get<UserClass>();

Add a new class called UserClass

1
2
3
4
5
6
7
8
namespace WebApplication1
{
public class UserClass
{
public string Name { get; set; }
public int Age { get; set; }
}
}

So, we can get the information from "UserSetting" in appsettings.json and serialize it to "UserClass" type.

Result:
x4


Use configurations in controller or other classes.

We have already known how to use configurations in Program.cs. Now we’d use it in a controller class.

IConfiguration

We need to use IConfiguration in the constructor of the controller class.

1
2
3
4
5
public AControllerClass(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }

For example,

Add IConfiguration parameter into constructor of WeatherForecastController.

1
2
3
4
5
6
7
8
9
10
11
private readonly ILogger<WeatherForecastController> _logger;
public IConfiguration Configuration { get; }
/// <summary>
/// This is Constructor
/// </summary>
/// <param name="logger"></param>
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
{
_logger = logger;
Configuration = configuration;
}

Use configurations in functions.

1
2
3
4
5
6
7
8
9
10
/// <summary>
/// Get Weather
/// </summary>
/// <returns></returns>
[HttpGet(Name = "GetWeatherForecast")]
public void Get()
{
//use configuration as the same ways used in Program.cs
var x1Controller = Configuration["Test"];
}

Next: .Net 6 + WebApi Basics 6

Last: .Net 6 + WebApi Basics 4


Good Day
😎