In .net core 8.0, we do not have startup.cs, and all things are merged into program.cs.
Program.cs in .NET 8.0
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Key points
- Earlier we had a program.cs and startup.cs separately and program.cs take responsibility for loading application settings / Logging/IIS profiles, etc, which do not need to change every time but startup.cs contains settings related to the application DI container, Application pipeline, and middleware.
- In .Net8.0 we have only program.cs and are responsible for application bootstrap.
- We can Add Services using builder.Services.
Here, we can do DI
- Adding Configuration using builder.Configuration.
- Configure Logging using builder.Logging.
- We can use the below code for middleware.
var app = builder.Build();
- Here, the app is an object of the Web Application class, which takes responsibility for middleware /pipeline.
- When we run this application, all settings get loaded into the builder object.
The below things get loaded when we run the application.
- Configuration
- Logging
- Env variables
- DI services
- Host related settings