ASP.NET Core Startup Class and Program.cs
Introduction.
Global.asax is no more in ASP.NET Core application. Startup.cs file is a replacement of Global.asax file in ASP.NET Core.
Startup.cs file is entry point of application level it handle request pipeline. Startup class which triggers at first when application launches.
Description.
Now question is startup.cs file is mandatory or not? Yes, startup.cs is mandatory, it can be decorated with any access modifier like public, private, internal. multiple Startup classes are allowed in a single application. ASP.NET Core will select the appropriate class based on its Enviroment.
If a class Startup{EnvironmentName} exists, that class will be called for that EnvironmentName.
Should we need to define class name with startup.cs? No it is not necessary that class name should be Startup.
We can define two method in startup file like ConfigureServices and Configure.
Startup file example :
public class Startup
{
// Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
}
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
...
}
}
ConfigureServices Method :
This is an optional method in Startup class which is used to configure services for application. when any request come to application ConfigureService method will be called first.
ConfigureServices method includes IServiceCollection parameter to register services. This method must be declared with a public access modifier, so that environment will be able to read the content from metadata.
ASP.net core has built-in support for Dependency Injection. We can add services to DI container using this method. Following are ways to define ConfigureServices method in startup class.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
Configure Method :
The Configure method is used to specify how the application will respond in each HTTP request. this method is mostly used for registering middleware in HTTP pipeline. this method method accept IApplicationBuilder parameter along with some other services like IHostingEnvironment and ILoggerFactory. Once we add some service in ConfigureService method, it will be available to Configure method to be used.
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
Above example shows to enable MVC feature in our framework we need to register UseMvc() into Configure and also need to register service AddMvc() into ConfigureServices.
Constructor for Startup Class :
We can specify parameterized constructor for startup file like IApplicationBuilder, IHostingEnvironment, IloggerFactory.
IApplicationBuilder : this interface contain properties and method related to current Enviroment.
IHostingEnvironment : this interface contain properties and method related to web hosting environment on which application is running.
IloggerFactory : this interface provides configuration for the logging system.
Program.cs :
ASP.NET Core Program class file is place where we can create a host for the web application.
Example :
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.Build();
}
WebHost is used to create instance of IWebHost and IWebHostBuilder. The CreateDefaultBuilder() method creates a new instance of WebHostBuilder.
the UseKestrel() specify method is extension, which specifies Kestrel as an internal web server. The Kestrel is an open-source, cross-platform web server for ASP.NET Core.
ASP.net Core is cross-platform and open-source and also it is compatible to host into any server rather than IIS, external web server such as IIS, Apache, Nginx etc.
When we want to host our application into iis we need to add UseIISIntegration() method specifies the IIS as the external web server.
UseStartup<startup>() method specifies the Startup class to be used by the web host. we can also specify our custom class in place of startup.
Build() method returns an instance of IWebHost and Run() starts web application till it stops.
Program.cs in ASP.NET Core makes it easy for us to setup a web host.
Nice explanation about the subject.
ReplyDeleteThank You :)
DeleteGreat Article
ReplyDeleteFinal Year Projects for CSE in Dot Net
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
Dot Net Training in Chennai
Thank you..
DeleteThe most important benefit of digital marketing is the traffic increase and online exposure that a startup business gets. It helps a startup build a strong relationship with its customer base and improve the brand in the process - which means a startup brand is spread across the internet at an extremely affordable cost.Startup in Bangalore
ReplyDeleteThe guidelines are adjusting, the type of console design is radically evolving, the presumptions we have concerning the primary design limits in creating a new activities machine must be re-evaluated.... Cryptocurrency
ReplyDelete