• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, August 18, 2020

ASP.NET Core OutOfProcess Hosting

 Admin     August 18, 2020     .Net, .Net Core, Asp.Net, C#     No comments   

In this article, I am going to discuss ASP.NET Core OutOfProcess Hosting Model in detail. I strongly recommended you to read ASP.NET Core InProcess Hosting and The Kestrel Web Server in ASP.NET Core Application articles before proceeding to this article. As part of this article, we are going to discuss the following pointers in detail.
  1. How to Configure OutOfProcess Hosting in ASP.NET Core?
  2. What is ASP.NET OutOfProcess Hosting?
  3. How does the OutOfProcess Hosting works in ASP.NET Core?
  4. Can we run an asp.net core application without using the built-in kestrel web server?
  5. If Kestrel can be used by itself as a web server which can directly handle and process the incoming HTTP Request, then why do we need a reverse proxy server?
Before understanding the OutOfProcess hosting, let us first have a look at the InProcess hosting model.

ASP.NET Core InProcess Hosting
Let first have a look on in Process Hosting before proceeding to Out Of Process Hosting

As we already discussed, to configure the InProcess hosting in ASP.NET Core application, you need to add the element to the application’s project file with a value of InProcess as shown below
ASP.NET Core Out Of Process Hosting Project File

In ASP.NET Core, with InProcess Hosting Model our application is going to be hosted in the IIS worker process (i.e. w3wp.exe in case of IIS server or iisexpress.exe if the hosting server is IISExpress). The most important point that you need to remember is we have only one web server i.e. IIS Server in case of InProcess hosting which is going to host our application as shown in the below image.
InProcess Hosting in ASP.NET Core

How to Configure the OutofProcess Hosting in ASP.NET Core Application?
We can configure the Out Of Process Hosting in two ways in ASP.NET Core.

Way1:
In this case, you just need to add the element to the applications project file with a value of OutOfProcess as shown below.
OutofProcess Hosting in ASP.NET Core

Way2:
The default hosting in ASP.NET Core is OutOfProcess Hosting. That means if you remove the  element from the application’s project file, then by default OutOfProcess hosting will be used.

What is Out of Process Hosting in ASP.NET Core?
In the case of the ASP.NET Core OutOfProcess Hosting Model, there are two web servers.
  1. An internal webserver which is the Kestrel web Server
  2. And an external web server which can be IIS, Apache, and Nginx.
The most important point that you need to keep in mind is depending on how you are running your application with the OutOfProcess hosting model, the external web server may or may not be used.

As we already discussed that the Kestrel web server is a cross-platform web server that is already embedded with your ASP.NET Core application. So if you are using the Out of Process Hosting model for your asp.net core application, then the Kestrel web server can be used in one of the following ways.

Way1:
We can use the Kestrel Web Server as the internet-facing web server which will directly process the incoming HTTP requests. In this scenario, only the Kestrel Server is used and the other one i.e. external web server is not going to be used. So when we run the application using the .NET core CLI then Kestrel is the only web server that is going to be used to handle and process the incoming HTTP request as shown in the below image.
Kestrel Server Out Of Process Hosting

To confirm this, open command prompt and run the application as shown in the below image
running dotnet core application using .net core cli

Now open the browser window and navigate to the following URL

http://localhost:5000

And here you will see the worker process name as dotnet as shown below
Kestrel worker process name

So, in this case, Kestrel is the only server that will handle and process the incoming HTTP Request. The following code of the Startup class displays the worker process name.
Worker Process name

Way2:
The Kestrel Web Server can also be used with the combination of a reverse proxy server such as IIS, Apache or Nginx. Now the question that comes to your mind is

If Kestrel can be used by itself as a web server which can directly handle and process the incoming HTTP Request, then why do we need a reverse proxy server?

This is because the reverse proxy server provides an additional layer of configuration and security which is not available with the Kestrel Server. It also maintains the load balancing. So it is a good choice to use Kestrel Server along with a reverse proxy server.

So when we use Kestrel Server along with a reverse proxy server, then the reverse proxy server will receive the incoming HTTP requests from the client and then forwards that request to the Kestrel server for processing. Once the Kestrel Server process that request, then it sends the response back to the reverse proxy server which then sends the response back to the requested client over the internet as shown in the below image.
External Proxy Server with Internal Kestrel Server

In our upcoming articles, we will discuss how to deploy an ASP.NET Core application to IIS and how we can use IIS as a reverse proxy server.

When we run the application directly from Visual Studio, then by default Visual Studio uses IIS Express. Now change the AspNetCoreHostingModel element value as shown below in the application’s project file.
OutOfProcess
As we have configured Out of Process hosting model, now the IIS Express acts as a reverse proxy server and Kestrel acts as the internal webserver.

Now, the IIS Express receives the incoming HTTP request and then forwards it to the Kestrel Web Server for processing. The Kestrel Web Server processes the request and sends the response back to the IIS Express which in turn sends the response back to the client i.e. to the browser.

Now run the application, and you will see the worker process as dotnet. So when you are using Out of Process Hosting model, then the Kestrel Web Server is going to hosts the application and process the request irrespective of whether you are using a reverse proxy server or not.

One more thing that you need to keep in mind is, when you are running your application using the .NET Core CLI, then by default, it ignores the hosting setting that you specified in the application’s project file i.e. csproj file. So, in that case, the value of the AspNetCoreHostingModel element is going to be ignored.

The .NET Core CLI always uses OutOfProcess Hosting Model and Kestrel is the webserver that will host the ASP.NET Core application and also handles the HTTP requests.

Can we run an asp.net core application without using the built-in kestrel web server?
YES. When we use the InProcess Hosting model, then the application is hosted inside the IIS worker process i.e. w3wp.exe in the case of IIS and iisexpress.exe in the case of IIS Express. That means the Kestrel Web Server is not used with the InProcess hosting model.

In the next article, we will discuss the launchSettings.json file in the ASP.NET Core application. Here, in this article, I try to explain the ASP.NET Core OutOfProcess Hosting model in detail. I hope this article will help you to understand the OutOfProcess Hosting model in ASP.NET Core Application.

Summary:
I hope this post will be helpful to understand the concept of ASP.NET Core OutOfProcess Hosting
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape