Hosting Models in ASP.NET Core 3.1 Applications

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? And the answer 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".

So whenever you create an ASP.NET Core application it by defaults contains an internal server provided by a .NET Core which is called as Kestrel. Due to this server, we can run ASP.NET Core apps on any platform like Windows, Mac or Linux. Before getting in details about hosting models lets first see what is Kestrel server.

What is the Kestrel Server?

According to the ASP.NET core docs:

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the webserver that's included by default in ASP.NET Core project templates.

Kestrel is based on the libuv library, the same library which is used by Node. Some features of Kestrel,

  • It supports SSL
  • Supports Http/2
  • 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 .NetCore 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 Kestrel server directly as user request facing server or we can deploy the app into IIS which will act as a proxy server and sends requests to 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.

out-of-process-2.png

Using Proxy Server

Due to limitations of Kestrel server, we can not use this in all the apps so 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 like one is IIS and another is Kestrel.

out-of-process-1.png

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 as 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 Kestrel server to host app 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.

in-process.png

We have discussed different types of hosting models. Now let 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 into ASP.NET then you can check steps here.

Run the application on IISExpress server then open the browsers network tab and check for the first call. Under the server section, you will able to see its showing Microsoft IIS.

iisexpress.PNG

Now open the command prompt and run the same application using dotnet CLI bu using command dotnet run. Now it will host app on http://localhost:5000.

dotnet-run.PNG

Browse the URL and open network tab and see the server attribute as Kestrel.

kestrel.PNG

You may have one question in your mind like where are these type defines? There are two ways to define the models:

  • .csproj file
  • 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 deafults 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 about the different type 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.

You can follow me on twitter @sumitkharche01.

Happy Coding!

No Comments Yet