Monday 18 January 2021

Part 5 : Hosting Models in Asp.Net Core 3.1

#10 minutes of reading


In this article I will show you hosting models in Asp.Net Core 3.1. Hosting is nothing but deploying your web application on web server. Dot net old framework host all web application on IIS only. But now with the help of Core, web application can host on multiple platform (web servers).

Before Core 2.2, kestrel only act as web server or internal web server. Kestrel is cross platform server default provided by core framework. As kestrel lags some of functionality compare IIS/Apache and NGINX. But again reason kestrel is fast because it provides limited functionality.

Either you can deploy web application directly on kestrel or if you want all functionality of fully grown web server, use external web server (IIS, NGINX or Apache) with kestrel(internal web server) explain in details later in this article. 

There are two hosting model Asp.Net Core provides from Core 2.2 onwards.

In-Process:

                It is default hosting process model in asp.net core 3.1 and IIS used as web server to host web application.This hosting model is specifically designed for IIS web server and Kestrel web server in  not involved here.



Out-Of-Process:

                It is not default hosting model. To host application with this hosting model, need to inform application by configuring Out-Of-Process in project file. By default Kestrel is involved in this hosing model and there are two scenarios to host application.

·        1. Kestrel act as main web server where kestrel communicating with internet for incoming request to Asp.Net core application. It is lightweight as limited services and provide high performance.


 

·       2. IIS/Apache/NGINX used as proxy server. In this scenario, two web servers involved. Kestrel is internal web server cross platform and IIS/Apache/NGINX as external web server. Here external web server act as reverse proxy server and redirect incoming request from internet to internal(kestrel) web server.

· Let’s implement both hosting model in asp.net core application. I’m referring project created in previous article. Click on start up class of project and add below highlighted code.


 

As we discussed In-Process is default hosting model in Asp.Net Core 3.1. Let’s run application and check what is show on browser window. Click on green triangle to execute application and make sure IIS express selected after icon.

It shows IISExpress as process name, it uses IIS express while running application from visual studio once you deploy application on IIS it will show process name as w3wp.exe (for IIS)

Now let’s configure Out-Of-Process hosting in application. Double click on project name to open project file from visual studio. In Core, project file is very light weight and no reference for files under project. It considers everything in root directory is included in project. No concept of include or exclude item from project like available in old .Net framework projects.


Add above highlighted line of code for adding Out-Of-Process hosting. Click on Asp.NetCore_sample near to green triangle highlighted in above screen.


Now it is showing process name as Asp.NetCore_sample which is nothing, but name of project and kestrel is used as web server.

Command prompt showing same output and port number when you run application from .Net CLI, it means if you’re executing application from .Net CLI it again uses kestrel as web server.

Now I have reverted back application to its default state. 


Run application using .Net CLI developer command prompt. Go to start menu expand visual studio 2019 folder and open Developer command prompt. Please refer blog 3 on .Net CLI for more details.


Navigate to root folder of project and execute dotnet run command. Application hosted on port 5001 and 5000 highlighted above.


It should display IIS Express but showing project name, it means if you’re running application from .NET CLI it ignore the hosting configured and use Kestrel as web server. As we discussed in previous article ConfigureWebHostDefaults is used for configuring IIS/Kestrel based on hosting model choosed. If hosting model is In_process IIS is only used as web server and for Out-Of-Process Kestrel will be the internal or main web server.  


Here I shown you different process name, for IIS Express it shows iisexpress process name on browser and for Kestrel project name. Even we can check this on request on browser, please be little patience will show you in near future with routing article.


Note : Kestrel is Cross platform support because with the help of Kestrel as internal web server core web application can host on (IIS, Apache, … etc). IIS, apache or NGINX used as external web server.

Previous                                                                                                                                                 Next

1 Comments:

At 29 January 2021 at 06:15 , Blogger Neha Puri said...

Good information about core hosting.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home