In this article, we will be discussing different hosting models provided by ASP.NET Core 3.1. Once you successfully developed your web application, what should be the next step you have to do? The answer is Hosting. We have to host our application to the server so that other people can access it. 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. Before getting into the details about hosting models, let's first see what is the Kestrel server.
Kestrel is based on the
libuv library, the same library which is used by Node.
Some features of Kestrel:
- It supports SSL
- It supports SSL
- lightweight
- cross-platform
Hosting Models in ASP.NET Core
There are 2 types of hosting models in ASP.NET Core i.e In-process Hosting and Out-of-process Hosting. Before ASP.Net Core 2.2 we have only one hosting model which is Out-of-process but after due to the performance we have In Process Hosting Model in 2.2+ versions.
Out-of-process Hosting Model
In Out-of-process hosting models, we can either use the Kestrel server directly as a user request facing server or we can deploy the app into IIS which will act as a proxy server and sends requests to the internal Kestrel server. In this type of hosting model we have two options:
Using Kestrel
So in this type Kestrel itself acts as edge server which directly server user requests. It means that we can only use the Kestrel server for our application.
Due to limitations of the Kestrel server, we can not use this in all the apps. In such cases, we have to use powerful servers like IIS, NGINX or Apache. So, in that case, this server acts as a reserve proxy server which redirects every request to the internal Kestrel sever where our app is running. Here, two servers are running. One is IIS and another is Kestrel.
This model is a default model for all the applications implemented before .NET Core 2.2. But there are some of the limitations of using this type such as performance slowness.
In-process Hosting Model
After the release of .NET Core 2.2, it introduced a new type of hosting which is called In-process hosting. In this type, only one server is used for hosting like IIS, Nginx or Linux. It means that the App is directly hosted inside of IIS. No Kestrel server is being used. IIS HTTP Server (IISHttpServer) is used instead of the Kestrel server to host apps in IIS directly. ASP.NET Core 3.1 onwards **In-process** hosting model is used as a default model whenever you create a new application using an existing template.
We have discussed different types of hosting models. Now let's see how to check which hosting model is being used. To do this, we have to first create a new ASP.NET core application. If you are new to ASP.NET, you can check the steps
here.
Run the application on the IISExpress server, then open the browsers network tab and check for the first call. Under the server section, you will able to see it showing Microsoft IIS.
Stop the app and open the command prompt and run the same application using dotnet CLI bu using the command dotnet run. Now it will host app on http://localhost:5000.
Browse the URL and open the network tab to see the server attribute as Kestrel.
Where are these types defined? There are two ways to define the models:
- In.csproj file
- In web.config file
In .csproj file
Open the .csproj file and add the below property to apply the proper hosting model.
- <PropertyGroup>
- <TargetFramework>netcoreapp3.1</TargetFramework>
- <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
- </PropertyGroup>
So the value <AspNetCoreHostingModel> property is case insensitive and if we don't define this property then it by default consider as In-process hosting model.
In web.config
In ASP.NET Core apps we don't have `web.config` so first, we have to publish the app and in the published folder you can see the `web.config` the file generated by ASP.NET Core. Now published the app you have created earlier and open the config file. It looks like this:
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <location path="." inheritInChildApplications="false">
- <system.webServer>
- <handlers>
- <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
- </handlers>
- <aspNetCore processPath="dotnet" arguments=".\DotNetCoreApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
- </system.webServer>
- </location>
- </configuration>
As I mentioned earlier from ASP.NET Core 3.1, the **In-process** hosting model is the default model. So if you want to change it to other i.e. Out -of-process then you just need to change `hostingModel="OutOfProcess" `.
Conclusion
In this article, I have explained the different types of hosting models provided by ASP.NET Core(i.e In-process & Out-of-process). I really hope that you enjoyed this article, and please do not hesitate to send me your thoughts or comments about what could I have done better.