• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, August 18, 2020

ASP.NET Core Main Method

 Admin     August 18, 2020     .Net, .Net Core, Asp.Net, C#     No comments   

In this article, I am going to discuss the ASP.NET Core Main Method in detail. Please read our previous article where we discussed the ASP.NET Core Project File in detail. As part of this article, we are going to discuss the following two important concepts in detail.
  1. The significance of the ASP.NET Core Main Method.
  2. Why do we have a Main() method in ASP.NET Core?
  3. What happens behind the scenes when you run a .NET core application?
In order to understand the Main method of ASP.NET Core application, let’s first create an Empty ASP.NET Core Web application.

Creating ASP.NET Core Web Application
Open Visual Studio 2017. Then select File => New => Project from the file menu as shown below.
ASP.NET Core Main Method First ASP.NET Core Project

From the “New Project” window, from the left pane expand the “Installed” template section. Then expand the “Visual C#” section and then select .NET Core. From the middle pane, you need to choose ASP.NET Core Web Application. Provide a meaningful name for your application such as “FirstCoreWebApplication”. Then provide the location where you want to create the Project. Finally, click on the OK button as shown in the image below.
FirstCore Web Application

Once you click on the OK button, it will open the following window. From the below window, select ASP.NET Core 2.2 (currently it is the latest version). Then uncheck the Configure for HTTPS checkbox. Then click on the OK button as shown below.
ASP.NET Core Main Method

Once you click on the OK button, it will create the ASP.NET Core Web Application with the following structure.
ASP.NET Core Empty Web Application Project Structure

As you can see from the above image, we have a file with the name Program.cs. The Program.cs file of our ASP.NET Core Web Application contains the following code.
ASP.NET Core Program Class

From the above image, you can see that the Program class contains a public static void Main() method. As we already know, when we create a console application in .net then by default the .NET Framework creates a class (i.e. Program class) with the Main Method. We also know that the Main() method is the entry point for that console application execution.

Now the question is, here we are not creating a console application, here we create an ASP.NET Core Web Application. Then why do we have a Main() method in the ASP.NET Core Web Application?

Why do we have a Main() method in ASP.NET Core?
The most important point that you need to keep in mind is, the ASP.NET Core Application initially starts as a Console Application and the Main() method is the entry point to the application.

So, when we execute the ASP.NET Core application, first it looks for the Main() method and this is the method from where the execution starts. The Main() method then configures the ASP.NET Core and starts it. At this point, the application becomes an ASP.NET Core web application.

If you look at the body of the Main() method, then you will find that it makes a call to the CreateWebHostBuilder() method by passing the command line arguments as shown in the below image.
Main Method Of ASP.NET Core Application.

As shown in the below image, the CreateWebHostBuilder() method returns an object that implements the IWebHostBuilder interface.
Create a Web Host Builder method

Within the Main() method, on this IWebHostBuilder object, the Build() method is called which actually builds a web host. Then it hosts our asp.net core web application within that Web Host.

Finally, on the web host, we called the Run() method, which actually runs the web application and it starts listening to the incoming HTTP requests.

CreateWebHostBuilder() method calls the static CreateDefaultBuilder() method of the WebHost class. The CreateDefaultBuilder() method creates a web host with the default configurations.

Behind the scene, to create a web host, the CreateDefaultBuilder() method does several things. In the next article, we will discuss the CreateDefaultBuilder() method in detail.

For now, just understand that the CreateDefaultBuilder() method sets up a web host with default configurations.

Startup Class
While setting up the web host, the Startup class is also configured using the UseStartup() extension method of the IWebHostBuilder class. The Startup class has two methods as shown below.
ASP.NET Core Startup Class

The ConfigureServices() method of the Startup class configures the services which are required by the application. The Configure() method of the Startup class sets up the pipeline of the application’s request processing. In a later article, we will discuss these two methods in detail.

In the next article, I will discuss the ASP.NET Core InProcess Hosting in detail. Here, in this article, I try to explain the ASP.NET Core Main Method in detail. I hope you understood the need and use of the Main Method in ASP.NET Core Web Application.

Summary:
I hope this post will be helpful to understand the concept of ASP.NET Core Main Method
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape