ASP.NET Core launchSettings.json file

ASP.NET Core launchSettings.json file

In this article, I am going to discuss the use and importance of the ASP.NET Core launchSettings.json file in detail. Please read our previous article where we discussed the OuOfProcess Hosting Model in ASP.NET Core Web Application. In order to understand the ASP.NET Core launchSettings.json file, let us first create a new ASP.NET Core application with an empty template.

Creating a new Empty ASP.NET Core Web Application

First, open Visual Studio 2019 and then click on the “Create a new project” box as shown in the below image.

Creating a new Empty ASP.NET Core Web Application

Once you click on the Create a new project box, then it will open the Create a new project window. From this window, you need to select the ASP.NET Core Web Application template and then click on the Next button as shown in the below image.

ASP.NET Core Web Application template

Once you click on the Next button, then it will open the “Configure Your New Project” window. Here, you need to give the name for your project, set the location of your project, give the solution name. In this example, I will give the name “FirstCoreWebApplication” and then click on the Create button as shown in the image below.

Configure Your New Project

Once you click on the create button, then it will open the Create a new ASP.NET Core Web Application window. From this window, you need to select the Empty Project template and uncheck all the checkboxes from the Advanced section. Please make sure to select .NET Core and ASP.NET Core 3.1 from the respective dropdown list and finally click on the Create button as shown in the below image.

Create a new ASP.NET Core Web Application

Once you click on the Create button, it will take some time and will create the Empty ASP.NET Core Web Application with the following file and folder structure.

Empty ASP.NET Core Web Application

As you can see from the above image, the ASP.NET Core Project has a file called launchSettings.json within the Properties folder. So, let us discuss the need and importance of this launchSettings.json file in the ASP.NET Core application.

Understanding LaunchSettings.json file in ASP.NET Core

The settings that are present within this file are going to be used when we run the .NET core application either from Visual Studio or by using .NET Core CLI.

The most important point that you need to keep in mind is this launchSettings.json file is only used within the local development machine. That means this file is not required when we publishing our asp.net core application to the production server.

If you have certain settings and you want your application to use such settings when you publish and deploy your asp.net core application to the production server, then you need to store such settings in the appsettings.json file. Generally, in the ASP.NET Core application, the configuration settings are going to be stored in the appsettings.json file. In our upcoming article, we will discuss the appsettings.json file in detail.

ASP.NET Core Application Profile settings in the launchSettings.json file:

If you open the launchSettings.json file, then by default you will find the following code or you can say settings within that file in ASP.NET Core 3.1 applications.

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:50409",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "FirstCoreWebApplication": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

In the above launchSettings.json file, within the profiles, we have two sections i.e. IIS Express and FirstCoreWebApplication as shown in the below image.

ASP.NET Core Application Profile settings in the launchSettings.json file

The point that you need to remember is when you run the application from Visual Studio either by pressing CTRL + F5 or just F5 then by default the profile with “commandName”: “IISExpress” is going to be used. On the other hand, if you run the ASP.NET Core application using .NET Core CLI (i.e. dotnet run command), then the profile with the “commandName”: “Project” is going to be used.  

However, if you want then you can choose which profile to use when you run the application by pressing CTRL + F5 or just F5, by clicking on the drop-down list in Visual Studio as shown below

ASP.NET Core launchSettings.json file

The value of the commandName property of the launchSettings.json file can be any one of the following.  

  1. IISExpress
  2. IIS
  3. Project

The CommandName property value of the launchSettings.json file along with the AspNetCoreHostingModel element value from the application’s project file will determine the internal and external web server (reverse proxy server) that are going to use and handle the incoming HTTP Requests. For better understanding, please have a look at the below table.

Internal and External Web Server in ASP.NET Core

Modifying the Configure method of Startup class

Modify the Configure method of the Startup class file as shown below to display the Name of the worker process that is going to handle the request in the browser window.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapGet("/", async context =>
        {
            await context.Response.WriteAsync("Worker Process Name : " +
                System.Diagnostics.Process.GetCurrentProcess().ProcessName);
        });
    });
}
Case1: CommandName as Project

When we use the CommandName as Project in the launchSettings.json file, then ASP.NET Core is going to ignore the AspNetCoreHostingModel value. The Kestrel is the only server that is going to host the application and handle the incoming request. Let’s prove this. Now, we need to set the launch Profile as FirstCoreWebApplication as shown below.

CommandName as Project

If you look at the launchSettings.json file, then you will see that the FirstCoreWebApplication profile uses the “commandName”: “Project” value, and as well as please focus on the application URL as shown below. In my application the URL is http://localhost:5000 and the port number might be varying in your example.

launchSettings.json file in ASP.NET Core

Now change the AspNetCoreHostingModel element value to InProcess in the application’s project file as shown below.

<Project Sdk="Microsoft.NET.Sdk.Web">
 <PropertyGroup>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
 </PropertyGroup>
</Project>

Now, if you run the project either by pressing CTRL + F5 or just F5, first it will launch the command prompt where it will host the application using the Kestrel server as shown below.

host the application using the Kestrel server

Once it launches the command prompt and hosts the application, then by default it opens your default browser and displays the worker process name as the project name i.e. FirstCoreWebApplication as shown in the below image. This is because when the CommandName value is Project then it ignores the AspNetCoreHostingModel value and Kestrel is the only server that is going to host and process the incoming requests.

run the application using the Kestrel server

Case2: CommandName as IISExpress and AspNetCoreHostingModel as InProcess

If we use the CommandName as IISExpress profile and if we set the AspNetCoreHostingModel value as InProcess in the applications project file, then IIS Express is the only server that is going to host and handle the incoming HTTP request. Let us prove this. First, use IIS Express as the lunch profile by selecting IIS Express from the drop-down list as shown below.

CommandName as IISExpress and AspNetCoreHostingModel as InProcess

Now, if you look at the launchSettings.json file, then you will see that the IIS Express profile use the “commandName”: “IISExpress” value and also please keep the focus on the application URL which is coming from the iisSettings section as shown below

CommandName as IISExpress and AspNetCoreHostingModel as InProcess

We already set the AspNetCoreHostingModel element value to InProcess. Now, when we run the application either by pressing CTRL + F5 or just F5, then it will display the value as iisexpress for the worker process name as shown in the below image and also have a look at the browser URL. This proves that IIS Express is the only web server that is going to host the application as well as handles the incoming HTTP Requests.

ASP.NET Core launchSettings.json file

Case3: CommandName as IISExpress and AspNetCoreHostingModel as OutOfProcess

If we use the CommandName as IISExpress profile and if we set the AspNetCoreHostingModel value as OutOfProcess then ASP.NET Core uses IIS Express as the external web server and Kestrel is the internal webserver. The external web server i.e. IIS Express will receive the incoming HTTP Requests and then forward the request to the internal web server i.e. Kestrel which is going to process the request. Let us prove this.

As we already set the launch profile as IIS Express, we just need to change the AspNetCoreHostingModel element value to OutOfProcess in the application’s project file as shown below.

<Project Sdk="Microsoft.NET.Sdk.Web">
 <PropertyGroup>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
 </PropertyGroup>
</Project>

That’s it. Run the application and it should display the project name as the worker process name as shown in the below image as the request is internally handled by Kestrel Web Server.

CommandName as IISExpress and AspNetCoreHostingModel as OutOfProcess

The rest two cases we will discuss in a later article when we host the application using IIS. If you want then you can also change the settings of the launchSettings.json file using the Graphical User Interface (GUI) provided by Visual Studio.

How to access the Graphical User Interface (GUI) in Visual Studio?

To do so, right-click on the project name in Solution Explorer and then select the “Properties” option from the context menu. Once you open the project properties window, click on the “Debug” tab on the as shown in the below image.

How to access the Graphical User Interface (GUI) in Visual Studio?

Using the Graphical User Interface, you can also change the settings of the launchSettings.json file. Now here you can see that the Environment Variable “ASPNETCORE_ENVIRONMENT” is set to “Development”. You can change this Environment Variable value to Staging or Production depending on where you are running your application.

If you want, then you can also add new environment Variables. These environment variables are available throughout your application. And if you want then you can also execute some code conditionally depending on the environment variables value. For example, consider the following Configure() method of Startup.cs file.

ASP.NET Core launchSettings.json

It checks if the environment is Development, then it is going to display the Developer Exception Page. In our upcoming articles, we are going to discuss more these environment variables. 

In the next article, I am going to discuss the ASP.NET Core Startup class in detail. Here, in this article, I try to explain the ASP.NET Core launchSettings.json file in detail. I hope this article will help you to understand the need and use of ASP.NET Core launchSettings.json file. 

9 thoughts on “ASP.NET Core launchSettings.json file”

  1. blank

    Hi,
    When we host the application as apservice in azure.
    Is the Kestrel server is only come in picture for handling request

  2. blank

    I am always confused with this configuration file and the appSettings.json file, I am not able to understand in everything what the Microsoft documentation describes. This tutorial really helps a lot, and it will be one of my favorite bookmarks.

    Thank you!

  3. blank

    Hello, I’m a problem… Can you help me? Come on, I need to access an api that I’m developing from another finish within my network, but when I publish it is always listened to by http://localhost:5000, and I need it was available on the ip of my machine, how do I make it work?

  4. blank

    We are trying to deploy a web api using load balancer (asp.net core 3.1 version), we have firewall access, connection is established .We are also able to access the web api using swagger ui individually from both the nodes while browsing it from IIS but not able to access from the common external URL…please help

  5. blank

    Excellent article again. I was experimenting with hosting a ASP.NET core web api application onto azure kubernates last night and in the yaml kubernates deployment file it deployed onto a NGINX which I had no clue what it was until tonight I understand that since the launchsetting.json file isnt used when it is deployed, it is using a OutOfProcess hosting which links the NGINX load balancer on top of the kestral within the microservice that I deployed my .NET Core web api application onto. Excellent article.

Leave a Reply

Your email address will not be published. Required fields are marked *