Header add


In this article we will discuss what is ASP .NET Core launchSettings.json file. If you miss our previous article about hosting model then please follow in below link.

ASP.Net Core Hosting model 
Kestrel Web Server in ASP.NET Core
Out Of Process Hosting

We alreday create a sample .NET Core application with Empty project. The application is target to ASP .NET Core 3.1. We cover below points in this article
  • What is launchSettings.json file in ASP .NET Core
  • Why it use in ASP .NET Core
What is launchSettings.json file in ASP .NET Core ?

This json file holds project specific settings associated with each debug profile, Visual Studio is configured to use to launch the application, including any environment variables that should be used. You can define framework for your project for compilation and debugging for specific profiles. LaunchSettings.json file is placed in Properties folder.

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 the asp.net core application to the production server.


launchSettings.json file have 2 sections. IIS Express and Your application name
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 , then the profile with the "commandName": "Project" is going to be used.

The value of the commandName property of the launchSettings.json file can be any one of the following.  IISExpress / IIS  / 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).



Modifying the Configure method of Startup class

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


When we use the CommandName as Project, 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 myFirstCoreDemo, If you look at the launchSettings.json file, then you will see that the myFirstCoreDemo profile uses the “CommandName”: ”Project” value and also keep the focus on the application URL as shown below.


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

<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

Now, if you run the project either by pressing CTRL + F5 or just F5, then it will display the value as myFirstCoreDemo for the worker process name. 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.

2nd Way

If we use the CommandName as IISExpress and the AspNetCoreHostingModel value as InProcess then IIS Express is the only server that is going to host and handle the incoming request. Let us prove this.

First, use IIS Express as the lunch profile by selecting IIS Express and in launchSettings.json file, you will see that IIS Express profile use the “CommandName”:”IISExpress” value.



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

<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

Now, if you run the project either by pressing CTRL + F5 or just F5, then it will display the value as iisexpress for the worker process name. This proves that IIS Express is the webserver that is going to host the application as well as handles the incoming HTTP Requests.

3rd Way

If we use the CommandName as IISExpress and 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 will receive the incoming HTTP Requests and then forward the request to the internal webserver which is going to process the request. So 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.

<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>

That’s it. Run the application and it should display myFirstCoreDemo  as the worker process name.

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

Right-click on the project name in Solution Explorer and then select the “Properties” option from the context menu. Click on the “Debug” tab on the project “Properties” window as shown below.


Using the Graphical User Interface, we 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


It checks if the environment is Development, then it is going to display the Developer Exception Page.



Summary
  In this tutorial we discussed ASP .NET Core launchSettings.json file. If have any question related to this topic then give your feedback.


You May Also Like...

Post a Comment

Previous Post Next Post