Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views5 pages

WebHost Concepts

The document discusses the Main method in ASP.NET Core applications and how it differs from console applications. The Main method is the entry point for ASP.NET Core apps and is used to configure the web host. It explains the hosting models of InProcess and OutProcess and how they affect performance and configuration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

WebHost Concepts

The document discusses the Main method in ASP.NET Core applications and how it differs from console applications. The Main method is the entry point for ASP.NET Core apps and is used to configure the web host. It explains the hosting models of InProcess and OutProcess and how they affect performance and configuration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

WebHost Concepts

https://dotnettutorials.net/lesson/asp-net-core-main-method/

From the above image, you can see that the Program class contains a public static void Main() method. As we already know,
when we create a console application in .net then by default the .NET Framework creates a class (i.e. Program class) with the
Main Method. We also know that the Main method is the entry point for that console application execution.

Now the question is, here we are not creating a console application, here we create an ASP.NET Core Web Application. Then
why do we have a Main() method in the ASP.NET Core Web Application?

Why do we have a Main() method in ASP.NET Core?

The most important point that you need to keep in mind is, the ASP.NET Core Web Application initially starts as a Console
Application and the Main() method is the entry point to the application. So, when we execute the ASP.NET Core Web
application, first it looks for the Main() method and this is the method from where the execution starts. The Main() method then
configures ASP.NET Core and starts it. At this point, the application becomes an ASP.NET Core web application.

If you further look at the body of the Main() method, then you will find that it makes a call to the CreateHostBuilder() method by
passing the command line arguments args as an argument as shown in the below image.

As shown in the below image, the CreateHostBuilder() method returns an object that implements the IHostBuilder
interface. The Host is a static class that can be used for creating an instance of IHostBuilder with pre-configured defaults. The
CreateDefaultBuilder() method creates a new instance of the HostBuilder with pre-configured defaults. Internally, it configures
Kestrel (Internal Web Server for ASP.NET Core), IISIntegration, and other configurations.

Within the Main() method, on this IHostBuilder object, the Build() method is called which actually builds a web host. Then it
hosts our asp.net core web application within that Web Host. Finally, on the web host, it called the Run()method, which will
actually run the web application and it starts listening to the incoming HTTP requests.
Hosting Model:

There are two Hosting model in .net core

InProcess
OutProcess
InProcess
When we create a new ASP.NET Core Web application by using any template, by default the project file is created
with InProcess hosting which is used for hosting the application in IIS or IIS Express scenarios.

How to Configure InProcess hosting in ASP.NET Core?


To configure the InProcess hosting for ASP.NET Core Web application there is only one simple setting, just add
the <AspNetCoreHostingModel> element to the application project file with a value of InProcess.

What happens when we set the Hosting model as InProcess?


In case of InProcess hosting (i.e. when the CreateDefaultBuilder() sees the value as InProcess for
the AspNetCoreHostingModel element in the project file), behind the scene the CreateDefaultBuilder() method
internally calls the UseIIS() method. Then host the application inside the IIS worker process (i.e. w3wp.exe for IIS
and iisexpress.exe for IISExpress).

From the performance point of view, the InProcess hosting model delivers significantly higher request throughout
than the OutOfProcess hosting model. Why we will discuss this at the end of this article.

What is IIS Express?


The IIS Express is a lightweight, self-contained version of IIS. It is optimized for web application development.
The most important point that you need to remember is we use IIS Express only in development, not on
production. In production we generally use IIS. In a later article, we will discuss how to deploy an ASP.NET Core
application on IIS.

Why InProcess Hosting Gives Better Performance that the OutOfProcess Hosting Model?

In the case of OutOfProcess hosting, there are 2 web servers


1. An internal web server and
2. One external web server.

The internal web server is called Kestrel and the external web server can
be IIS, Nginx, or Apache. With the InProcess hosting model, there is only one web server i.e. IIS. So,
in the case of the InProcess hosting model, we do not have the performance penalty for navigating
the requests between the internal and external web servers. This is the reason why the InProcess
hosting model delivers significantly higher request throughput than the OutOfProcess hosting model.

OutProcess:

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 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 .

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 should come 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.

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.

<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>

As we have configured the 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 your project name. 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.
Kestrel Server:

What is a Kestrel Web Server?


As we already discussed ASP.NET Core is a cross-platform framework. It means it supports to development and
run applications on different types of operating systems such as Windows, Linux, or Mac.

The Kestrel is the cross-platform web server for the ASP.NET Core application. That means this Server supports
all the platforms and versions that the ASP.NET Core supports. By default, it is included as the internal web
server in the .NET Core application.
The Kestrel Web Server is generally used as an edge server i.e. the internet-facing web server which directly
processes the incoming HTTP request from the client. In the case of the Kestrel web server, the process name
that is used to host and run the ASP.NET Core application is the project name.

As of now, we are using visual studio to run the ASP.NET Core application. By default, the visual studio uses IIS
Express to host and run the ASP.NET Core application. So, the process name is IIS Express that we already
discussed in our previous article.

IActionResult vs ActionResult
IActionResult is an interface and ActionResult is an implementation of that interface.

ActionResults is an abstract class and action results like ViewResult, PartialViewResult, JsonResult, etc., derive
from ActionResult.

Let's say you want to create an action result not catered to by MVC, say an XML result.

IActionResult is an interface, we can create a custom response as a return, when you use ActionResult you can
return only predefined ones for returning a View or a resource. With IActionResult we can return a response, or
error as well. On the other hand, ActionResult is an abstract class, and you would need to make a custom class
that inherits.

You might also like