Author : HASSAN MD TAREQ | Updated : 2020/08/01

Overview

  • An ASP.NET Core application is basically a console application that hosts a web server (Kestrel)
    • Starts executing from the entry point public static void Main() in Program class
    • The Main method is similar to the Main method of a console Applications
    • All the .NET Core applications basically are console application
    • We build other types of applications like MVC Web Application or Razor page application over the console app
  • The main purpose of the Program class is to configure the applications infrastructure
  • The program class creates the Web Host at the startup & then configures the logging, Dependency Injection Container. configuration system, Kestrel Web server, IIS Integration, etc.
  • The CreateDefaultBuilder() method creates a new instance of WebHostBuilder with pre-configured defaults
  • Links

Program.cs

    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>();
            });
    }

Serilog integration

Logger initialization in Program class

Seeding database

Database Seeder

Example of a complete Program class

  • Has Serilog configuration
  • Has Azure KeyVault configuration

Program.cs

using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace Blazor.Hovermind
{
    public class Program
    {
        public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();


        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();

            try
            {
                Log.Information("Running Blazor App");

                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
            {
                if (context.HostingEnvironment.IsDevelopment())
                { return; }

                var builtConfig = config.Build();

                config.AddAzureKeyVault($"https://{builtConfig["KeyVaultName"]}.vault.azure.net/");
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            }).UseSerilog();
    }
}