In the Core project, we have a file called. In this file, we have a public static void Main() method.
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => (args) .UseStartup<Startup>(); }
If you have any experience with the traditional .NET Framework, you know that the console application has the Main() method, which is the entry to that console program.
But today, we are creating a Core web application instead of a console application. So, one obvious question that comes to mind is. Why do we also have a Main() method?
So remember this knowledge point. The Core application is initially launched as a console application, and the Main() method in the file is the entry.
So when our application is executed at runtime, it looks for this Main() method and where the execution configuration begins.
This Main() method configures Core and starts it, at which point it becomes a Core web application. So if you keep track of the Main() method, it calls the CreateWebHostBuilder() method to pass command line parameters.
Then you can see that the CreateWebHostBuilder() method returns an object that implements IWebHostBuilder. On this object, calling the Build() method will generate and host our Core application on the server. The program on the server calls the Run() method, which runs the web application and starts listening for incoming HTTP requests. The CreateWebHostBuilder() method calls the static method CreateDefaultBuilder() in the static class WebHost. The CreateDefaultBuilder() method will create a preset default value on the server. The CreateDefaultBuilder() method performs multiple operations to create a server.
We discuss all the methods in CreateDefaultBuilder() in detail in the later video.
And now you only need to understand that the CreateDefaultBuilder() method exists when it is used to create the default value of the program configuration on the server. As part of setting up the server, it also uses the extension method of UseStartup() in the IWebHostBuilder interface to configure the Startup class. If you are not familiar with the concept of extension methods, then it is time for you to make up for it.
According to Microsoft's rules, the startup class in Core is called Startup. There are 2 methods for this class.
public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (()) { (); } (async (context) => { await ("Hello World!"); }); } }
C#
Although the Startup class has only two methods, these two methods do very important things:
ConfigureServices() method configures the services required by the application
Configure() method configures the application's request processing pipeline
It is very important for us to understand the role of these two methods.
In later articles, these two methods will be used in large quantities. At that time, everyone will need to deepen their impressions many times.
Thank you for your support.