ASP.NET Hosting Models
2021-11-21
Hosting
The process of deploying/installing an application into the server is called “Hosting”. Whenever you create an ASP.NET Core application, by default it contains an internal server provided by a .NET Core that is called Kestrel. Due to this server, we can run ASP.NET Core apps on any platform like Windows, Mac or Linux.
The new Program.cs file
builder.Services.xxx
can add Services into the Dependency Injection container.
- we can add custom services here as well, this is for registration purpose
- we can also include config files by calling
builder.Configuration.xxx
method - we can add logging provider here as well
- we can change the webhost to use a different root to provide static files
The app part was in the Startup.cs file, used to check environment, use static files, configure the routing..
- we can add our own middleware and pipeline configuration here.
- app can use both MVC and Razor pages
Top-level statement
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements
Top-level statements enable you to avoid the extra ceremony required by placing your program’s entry point in a static method in a class. The C# templates for .NET 6 use top level statements.
This new feature enables us to have a simpler Program.cs file for ASP.NET Core web applications!
Global Using
.NET 6 SDK has added a set of implicit global using directives which include the most common namespaces for the project type, so we don’t need to writing a lot of using statements.
For additional global using, we can create a separate file to put all the global using there, so our project look much cleaner.
Once you have a global using, the local using will be greyed out to indicate you don’t need that line of code.
OutOfProcess Hosting Model
- Forwarding web requests to a backend ASP.NET Core app running the Kestrel server, called the out-of-process hosting model .
InProcess Hosting Model - default
- Hosting an ASP.NET Core app inside of the IIS worker process (
w3wp.exe
), called the in-process hosting model .
There are trade-offs between each of the hosting models. By default, the in-process hosting model is used due to better performance and diagnostics.