InProcess Hosting in ASP.NET Core

By FoxLearn 1/14/2025 8:05:31 AM   9
In this article, we'll explore the In-Process Hosting Model in ASP.NET Core, explaining its concepts, how it works, and how to configure it in an application.

What is Hosting?

Hosting, or web hosting, is a service that stores your website’s data and makes it accessible on the Internet. Web hosting providers offer server space for rent, where your website's files, such as HTML, CSS, images, and documents, are stored, allowing your site to function online.

The web server, such as Apache, Nginx, or Microsoft’s IIS (Internet Information Services), is responsible for handling requests from users' browsers and serving the appropriate response.

Hosting Models in ASP.NET Core

In ASP.NET Core, the hosting model determines how an application is configured, how it interacts with web servers, and how HTTP requests are handled.

The two primary hosting models in ASP.NET Core are:

  • In-Process Hosting: The application runs inside the IIS worker process.
  • Out-of-Process Hosting: The application runs in a separate process, often in Kestrel, with IIS acting as a reverse proxy.

What is the In-Process Hosting Model?

In the In-Process Hosting Model, the ASP.NET Core application runs inside the IIS worker process (w3wp.exe). This model is the default for ASP.NET Core applications running on Windows with IIS or IIS Express. The main advantage of this model is its performance, as it avoids the overhead associated with communication between IIS and an external process.

How Does In-Process Hosting Work?

  1. User Request: The user sends an HTTP request to the web server (IIS).
  2. IIS: IIS receives the request. If the application isn’t running, it invokes the ASP.NET Core Module (ANCM).
  3. ASP.NET Core Module (ANCM): The ANCM checks whether the application is running. If not, it loads the .NET Core runtime and starts the application within the IIS worker process.
  4. IIS HTTP Server: After the application is running, IIS forwards requests directly to the application’s HTTP handler.
  5. Application Code: The ASP.NET Core application processes the request via its middleware pipeline, performing tasks such as authentication, routing, and data retrieval.
  6. IIS HTTP Server: Once the application has processed the request, the response is sent back to IIS.
  7. IIS: Finally, IIS sends the response back to the client’s browser.

When creating a new ASP.NET Core project, the default Program.cs file contains the Main() method, which is the entry point for your application. The method calls CreateBuilder(), which sets up the hosting environment.

namespace DemoWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var app = builder.Build();

            app.MapGet("/", () => "Worker Process Name : " + System.Diagnostics.Process.GetCurrentProcess().ProcessName);

            app.Run();
        }
    }
}

The CreateBuilder() method configures the web host by setting up the server (such as IIS or Kestrel), logging, configuration, and dependency injection. It also specifies whether to use the In-Process or Out-of-Process hosting model.

By default, ASP.NET Core projects targeting IIS are set up for In-Process hosting. To explicitly set the hosting model, you can modify the project file (.csproj) as follows:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>
</Project>

To use the In-Process Hosting Model in Visual Studio, you must first configure the launch profile to IIS Express. If In-Process hosting is enabled, the application will run directly within the IIS worker process (w3wp.exe or iisexpress.exe in development).

You can verify that the application is running within the IIS worker process by checking the process name in the browser or using the System.Diagnostics.Process.GetCurrentProcess().ProcessName statement in the Main method of Program.cs.

What is IIS Express?

IIS Express is a lightweight, self-contained version of IIS designed for local development. It provides a web server environment similar to the full IIS, allowing developers to build and test web applications locally before deployment.

Benefits of In-Process Hosting

  • Improved Performance: Running the application within the same process as IIS reduces the overhead of inter-process communication, resulting in faster response times and lower latency.
  • Simplified Deployment: In-Process hosting simplifies deployment since everything runs within IIS, avoiding the need for additional reverse proxy configurations.
  • Better Resource Utilization: Sharing the same process between IIS and the application minimizes resource consumption, improving scalability.

The In-Process Hosting Model is ideal for ASP.NET Core applications that require high performance, especially in environments where IIS is used as the web server. By running the application within the IIS worker process, this model reduces overhead and improves overall efficiency.