ASP.NET Core - Program.cs
ASP.NET Core web application is actually a console project which starts executing from the entry point
public static void Main()
in Program
class where we can create a host for the web application.Setup Host in ASP.NET Core 1.x:
The following is a typical Program.cs in ASP.NET Core 1.x.
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace MyFirstCoreApp
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
The WebHostBuilder class is the helper class to create and configure a host for a web application.
The UseKestrel()
method is an extension method which specifies Kestrel as an internal web server. The Kestrel is an open-source, cross-platform web server for ASP.NET Core. It is designed to be used behind proxy because it has not yet matured to be exposed as a full-fledge web server.
ASP.NET Core application can be a cross-platform application so it can be used with any web server, and not only IIS.
The UseContentRoot()
method specifies the current directory as a root directory which will be srcfolder in the default ASP.NET Core project.
The UseIISIntegration()
method specifies the IIS as the external web server or the reverse proxy server.
The UseStartup<startup>()
method specifies the Startup class to be used by the web host. Visual Studio creates Startup.cs by default with every new ASP.NET Core application. This Startup class is like Global.asax of .NET framework where you can configure request pipeline (middleware).
The Build()
method returns an instance of IWebHost using the configuration specified above.
The Run()
method starts the web application and blocks the calling thread till the host is shutdown.
Setup Host in ASP.NET Core 2.x:
The following is the Program
class in ASP.NET Core 2.x:
Program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace MyFirstCoreApp
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
The method expression BuildWebHost()
build web host with pre-configured defaults and it can be written as a method
that returns IWebHost
.
The
creates an instance ofCreateDefaultBuilder()
method creates a new instance ofWebHostBuilder
with pre-configured defaults alsoWebHostBuilder
and sets up Kestrel, content root directory, IIS integration which is same as ASP.NET Core 1.xMain()
method.
It also calls ConfigureAppConfiguration()
to load configurations from appsettings.json files, environment variables and user secrets. The ConfigureLogging()
method setup logging to console and debug window.
CreateDefaultBuilder()
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment())
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
}
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseIISIntegration()
.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
});
return builder;
}
No comments:
Post a Comment