Skip to main content

Lesson 1 of 8

WebApplication Builder - Minimal Hosting Model

ASP.NET Core .NET 6+ uses the minimal hosting model: a single Program.cs with WebApplication.CreateBuilder(). It replaces the old Startup.cs pattern - no more separate ConfigureServices and Configure methods. Everything is in one place, cleaner and simpler.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Modern Program.cs - Complete Setup

Modern Program.cs - Complete Setup
// Program.cs - .NET 8 minimal hosting model
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// ━━ 1. Configure logging FIRST (before any other builder setup) ━━
builder.Host.UseSerilog((ctx, config) =>
    config.ReadFrom.Configuration(ctx.Configuration));

// ━━ 2. Register services in DI container ━━
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Application services
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IProductRepository, ProductRepository>();

// Strongly-typed configuration (merged from dotnet-core course)
builder.Services.AddOptions<DatabaseOptions>()
    .Bind(builder.Configuration.GetSection(DatabaseOptions.Section))
    .ValidateDataAnnotations()  // validate at startup, not at runtime
    .ValidateOnStart();         // ← throws at startup if config invalid - fail fast!

// EF Core
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(
        builder.Configuration.GetConnectionString("DefaultConnection"),
        sqlOptions => sqlOptions.EnableRetryOnFailure(3)));

// JWT Authentication (see Module 7 for full setup)
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddAuthorization();

// ━━ 3. Build the application ━━
var app = builder.Build();

// ━━ 4. Configure MIDDLEWARE PIPELINE (order matters!) ━━
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/error");
    app.UseHsts(); // HTTP Strict Transport Security
}

app.UseHttpsRedirection();
app.UseSerilogRequestLogging(); // structured request logs
app.UseRouting();
app.UseAuthentication(); // must come BEFORE UseAuthorization
app.UseAuthorization();
app.MapControllers();
app.MapHealthChecks("/health");

// ━━ 5. Run ━━
app.Run();

// Placeholder types
interface IProductService { }
class ProductService : IProductService { }
interface IProductRepository { }
class ProductRepository : IProductRepository { }
class AppDbContext : Microsoft.EntityFrameworkCore.DbContext { }
class DatabaseOptions
{
    public const string Section = "Database";
    [System.ComponentModel.DataAnnotations.Required]
    public string ConnectionString { get; set; } = "";
}

Tip

Tip

Practice WebApplication Builder Minimal Hosting Model in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of WebApplication Builder Minimal Hosting Model from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with WebApplication Builder Minimal Hosting Model is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Finished this lesson?

0 of 8 in Module 2
Sign in to save progress

Topics in This Module

0/8 done