Working with the Program class

The Program class is the main entry point for ASP.NET Core 3 applications. In fact, ASP.NET Core 3 applications are very similar to standard .NET Framework console applications in this regard. Both have a Main method that is executed when running the application.

Even the basic signature of the Main method, which accepts an array of strings as arguments, is the same, as you can see in the following code. To no one's surprise, this is due to the fact that an ASP.NET Core application is, in reality, a console application hosting a web application:

   
    namespace TicTacToe 
    { 
      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>();
}); } }

Normally, you do not need to touch the Program class in any way. By default, everything necessary to run your application is already there and pre-configured.

However, you might want to activate some of the more advanced functionalities.

For instance, you could enable the capturing of errors during server startup and display an error page. In this case, you just have to use the following instruction:

    webBuilder.CaptureStartupErrors(true); 

By default, this setting is not enabled, which means that in case of errors, the host will just exit. This might not be the desired behavior and we recommend changing this parameter accordingly.

Two other useful parameters that work together are PreferHostingUrls and UseUrls. You can indicate whether the host should listen on the standard URLs defined by Microsoft.AspNetCore.Hosting.Server.IServeror-specific URLs you have provided. The URLs can have different formats depending on your needs, such as:

  • An IPv4 address with host and port (for example, https://192.168.57.12:5000)
  • An IPv6 address with port (for example, https://[0:0:0:0:0:ffff:4137:270a]:5500)
  • A hostname (for example, https://mycomputer:90)
  • A localhost (for example, https://localhost:443)
  • A Unix socket (for example, http://unix:/run/dan-live.sock)

Here is an example of how you could set those parameters:

    webBuilder.PreferHostingUrls(true);         
webBuilder.UseUrls("http://localhost:5000");

Here is an example of a Program class, which includes all of the concepts shown previously:

    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>();
webBuilder.CaptureStartupErrors(true);
webBuilder.PreferHostingUrls(true);
webBuilder.UseUrls("http://localhost:5000");
}); }

The default code in the Program class came into being from ASP.NET Core version 2.1, including version 3. Previous versions made use of WebHostBuilder, instead of a generic web host that we shall cover in the following section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.231.26