Asp.Net Core applications are self-hosted. The Host process in which it will be executed needs to implement the IWebHost interface.
This Host process / IWebHost implementation is in charge of Application Startup as well as it's Lifecycle. The Host process has to be configured
to use a particular HTTP Server which will handle the Request.
to use certain Middleware components for the Application Request-Response Pipeline.
to create the Services that will be needed by Application.
In the ASP.Net Core MVC Project Template, the Program.cs file comes with the following code which builds and launches the Host / IWebHost.
The IWebHost can be launched
- in a blocking manner by calling the Run() method.
- in a non-blocking manner by calling the Start() method.
HTTP Server
The Host process can use one of the following 2 HTTP Servers
- Kestrel (Cross Platform)
- WebListener (Windows Only)
This HTTP Server listens to the request and forwards it to our ASP.Net Core Application for execution.
In Production, these HTTP servers should not be external facing. They should be behind a Proxy/Reverse Proxy like IIS on Windows, Apache on Linux etc which will forward the request to it.
(IIS has an ASP.Net Core Module which forwards the request to the HTTP Server being used by Host of our ASP.Net Core application)
In the above code, the UseKestrel() method indicates the the Host process will use Kestrel as the HTTP Server. The UseIISIntegration() method indicates that IIS will be used as Reverse Proxy.
Configuring Middleware
The IWebHost can be configured by providing a Configure() method, or specifying a Startup class which will have the method.
The Configure() method is used to add Middleware components to the Application Request-Response Pipeline. Think of Middleware components as the lightweight .Net Core versions of .Net Framework's HttpModules. These Middleware Components perform certain actions on the incoming request and then pass it onto the next Middleware Component. Or they may handle the Incoming Request themselves and create and send back the Response instead of passing it on to the next Middleware Component.
In the ASP.Net Core MVC Project Template, the Startup.cs file comes with the following code in the Configure() methods adding some of the Middleware Components by default.
Configuring Services
The IWebHost Services can be configured by providing a ConfigureServices() method, or specifying a Startup class which will have the method.
In the ASP.Net Core MVC Project Template, the Startup.cs file comes with the following code in the ConfigureServices() methods adding some of the Services by default.