Author : MD TAREQ HASSAN
Overview
- Startup class contains configuration/initialization code in the form of the
ConfigureServices()
andConfigure()
methodsConfigureServices()
: register services that are required by the app & consumed across the app via dependency injection (DI)Configure()
: creates the app’s request processing pipeline
- The Startup class is specified when the app’s host is built by
UseStartup()
extension method of theIWebHostBuilder
inProgram.cs
- Named
Startup
by convention - The host provides services that are available to the Startup class constructor
- A common use of dependency injection into the Startup class is to inject:
HostingEnvironment
to configure services by environmentIConfiguration
to read configurationILoggerFactory
to create a logger inStartup.ConfigureServices()
- Links:
Startup.cs
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
- Optional method
- Called by the host before the Configure method to configure the app’s services
- To use any service in the app, call
Add{Service}
extension methods onIServiceCollection
(AddDbContext
,AddDefaultIdentity
,AddEntityFrameworkStores
,AddRazorPages
) - Adding services to the service container makes them available within the app and in the Configure method (The services are resolved via dependency injection or from ApplicationServices)
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
//... ... ...
}
public void Configure(IApplicationBuilder app)
{
//... ... ...
}
}
Configure
- Request handling pipeline / middleware pipeline is configured here
Use{Service}()
: adds one or more middleware components to the request pipelineRun{Service}()
: add terminal middleware- Each middleware component in the request pipeline is responsible for invoking the next component in the pipeline or short-circuiting the chain, if appropriate
- The request pipeline is configured by adding middleware components to an
IApplicationBuilder
instance - Hosting creates an
IApplicationBuilder
and passes it directly to Configure - Additional services, such as IWebHostEnvironment, ILoggerFactory, or anything defined in ConfigureServices, can be specified in the Configure method signature. These services are injected if they’re available
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//... ... ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
Multiple Startup
- When the app defines separate Startup classes for different environments (for example, StartupDevelopment), the appropriate Startup class is selected at runtime
- The class whose name suffix matches the current environment is prioritized
- If the app is run in the Development environment and includes both a Startup class and a StartupDevelopment class, the StartupDevelopment class is used. For more information
See: