D
D
Denis Bredun2020-08-05 23:12:59
ASP.NET
Denis Bredun, 2020-08-05 23:12:59

What is the significance of the arguments to the Main() method in creating an IHost object?

We have a Program class of an empty web application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ASP_NET_Core_Lessons
{
  public class Program
  {
    public static void Main(string[] args)
    {
      CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) => 
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            });
  }
}

Here is a list of questions:
1. What are the parameters of the Main() method, which are passed to the CreateHostBuilder(string[] args) method, and what are they (I heard that they are console strings) for?
2. What is the initial logic in these parameters? For application configuration?
3. What role do the args parameters of the Main() method play in this case, which we pass to the CreateHostBuilder(string[] args) method (more precisely, to Host.CreateDefaultBuilder(args))? What are they responsible for? What will happen if they are removed, and how will this affect the operation of the program?
Don't be surprised if I ask so many questions - I just started learning ASP.NET Core.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Bredun, 2020-08-05
@Luffy1

From the C# Programming Guide on MSDN :

The parameter of the Main method is a String array that represents the command line arguments

So, if I had a program (MyApp.exe) like this:
class Program
{
  static void Main(string[] args)
  {
    foreach (var arg in args)
    {
      Console.WriteLine(arg);
    }
  }
}

What I started on the command line is as follows:
MyApp.exe Arg1 Arg2 Arg3
In the main method, an array containing three strings will be passed: "Arg1", "Arg2", "Arg3".
If you need to pass an argument that contains a space, wrap it in quotes. For example: Command line arguments are commonly used when you need to pass information to your application at run time. For example, if you are writing a program that copies a file from one location to another, you will probably pass two locations as command line arguments. For example:
MyApp.exe "Arg 1" "Arg 2" "Arg 3"
Copy.exe C:\file1.txt C:\file2.txt
We need these parameters if we want to configure the application using the console - set the input parameters, using which the application will have dynamic behavior - we will be able to control the application logic, or rather, what operations will possibly be performed on, as in the location example file. That is, they have the same role as input parameters as regular methods - depending on these input parameters, we will also get a dynamic result.
And by passing these parameters to the CreateDefaultBuilder(string [] args) method, we configure the application's initializer. As the C# Programming Guide on MSDN says :
The following defaults apply to the returned HostBuilder:
Set the ContentRootPath to the result of GetCurrentDirectory()
Load host IConfiguration from prefixed environment variables "DOTNET_"
Load host IConfiguration from supplied command line arguments
Load application IConfiguration from "appSettings.json" and "appSettings.[EnvironmentName]. JSON
load application IConfiguration from user secrets when EnvironmentName is "Development" using input assembly
Load application IConfiguration from environment variables
Load application IConfiguration from supplied command line arguments
Configuring an ILoggerFactory for logging to console, debug, and event source output
includes scope checking in the dependency injection container if EnvironmentName is "Development"

And IConfiguration -
Represents a set of application configuration properties as key/value pairs.

And if we do not pass parameters, then they simply will not be taken into account when creating HostBuilder:
Method overload in which we do not pass parameters:
Set the ContentRootPath to the result of GetCurrentDirectory()
Load the IConfiguration node from the prefixed environment variables "DOTNET_"
Load the application's IConfiguration from "appSettings.json" and "appSettings.[EnvironmentName]
.json load the app's IConfiguration from user secrets when EnvironmentName is "Development" with input assembly
Loading applications' IConfiguration from environment variables
Setting the ILoggerFactory to log to console, debug, and event source output
includes scope checking in the DI container if EnvironmentName is "Development"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question