Application Initialization And Configuration In ASP.NET Versions

Most of us might have worked upon various versions of ASP.NET and few of you must be aware about the major changes happened in application initialization and configuration phase. In this article, I'll be outlining few of those major changes starting from ASP.NET MVC, ASP.NET Core 1.x and ASP.NET 2.x.

Most of us may have worked upon various versions of ASP.NET, and a few of you must be aware of the major changes that happened in the application initialization and configuration phase. In this article, I'll be outlining a few of those major changes starting from ASP.NET MVC, ASP.NET Core 1.x, and ASP.NET 2.x.
 
In ASP.NET

In the era of ASP.NET (prior to ASP.NET Core releases), the loading of the application was handled by IIS. Inetmgr used to call the web application's entry point, whereas Global.asax.cs used to provide the Application_Start() method. The below sample code snippet is taken from Global.asax.cx file.
  1. public class MvcApplication : System.Web.HttpApplication  
  2. {  
  3.         protected void Application_Start()  
  4.         {  
  5.             AreaRegistration.RegisterAllAreas();  
  6.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  7.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  8.             BundleConfig.RegisterBundles(BundleTable.Bundles);  
  9.         }  
  10. }  
In ASP.NET Core 1.x

Moving on to ASP.NET Core, Global.asax.cs doesn't exist anymore as the application initialization process itself has changed a lot. In the case of Core, almost all the application initialization and configuration related changes are taken care of by two important files named Program.cs and Startup.cs.
 
Program.cs

This file takes care of the web hosting part. Below is the sample code snippet.
  1. public class Program  
  2. {  
  3.         public static void Main(string[] args)  
  4.         {  
  5.             var host = new WebHostBuilder()  
  6.                 .UseKestrel()  
  7.                 .UseContentRoot(Directory.GetCurrentDirectory())  
  8.                 .UseIISIntegration()  
  9.                 .UseStartup<Startup>()  
  10.                 .Build();  
  11.   
  12.             host.Run();  
  13.         }  
  14.  }  
Startup.cs 

This file takes care of dependency injection and configuration related things. Below is the sample code snippet.
  1. public class Startup  
  2.     {  
  3.         public Startup()  
  4.         {  
  5.         }    
  6.   
  7.         // This method gets called by the runtime. Use this method to add services to the container.         
  8.         public void ConfigureServices(IServiceCollection services)  
  9.         {  
  10.             services.AddMvc();  
  11.             services.AddScoped<IStudentRepository, StudentRepository>();  
  12.             services.AddDbContext<ApplicationDbContext>          (c=>c.UserSqlServer(Configuraiton.GetConnectionString("PrimaryConnection")));

  13.         }  
  14.   
  15.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  16.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  17.         {  
  18.             loggerFactory.AddConsole();  
  19.             if (env.IsDevelopment())  
  20.             {  
  21.                 app.UseDeveloperExceptionPage();  
  22.             }  
  23.               
  24.   
  25.             app.UseMvc(configureRoutes);  
  26.             app.Run(async (context) =>  
  27.             {  
  28.                 await context.Response.WriteAsync("Hi There!");  
  29.             });  
  30.         }  
  31.   
  32.         private void configureRoutes(IRouteBuilder routeBuilder)  
  33.         {  
  34.             
  35.         }  
  36.     }  
In ASP.NET Core 2.x

In this version, application initialization and configuration still revolves around these two files named Startup.cs and Program.cs, but much more simplified and with reduced lines of code. Let's have a look at this new Program.cs file.
  1. public class Program  
  2. {  
  3.         public static void Main(string[] args)  
  4.         {  
  5.             BuildWebHost(args).Run();  
  6.         }  
  7.   
  8.         public static IWebHost BuildWebHost(string[] args) =>  
  9.             WebHost.CreateDefaultBuilder(args)  
  10.                 .UseStartup<Startup>()  
  11.                 .Build();  
  12. }  
If you notice the above snippet, it looks very compact. Just three lines, isn't it? Where did that entire configuration go which we use to specify in Core 1.x?

Well, that entire thing is wrapped up in a single line via CreateDefaultBuilder method. This method has many hidden things, including: 
  • configured the Kestrel along with integration with IIS
  • set the current project directory as the root content 
  • configured the logging system to make it readable from appsettings.json file
  • configured reading of environment variables from appsettings.json file, etc.
Isn't it a cleaner way?

Happy learning!