ASP.NET Core 3.1 in the Startup category

Startup Class configuration request pipeline services and applications.

Startup Class

ASP.NET Core applications use  Startup class, according to the agreed named  StartupStartup class:

  • It may optionally include  ConfigureServices  method to configure the service application. Services is a provider of application functionality reusable components. In  ConfigureServices the registration services, and through (DI) Dependency Injection  or  ApplicationServices  use the service throughout the application.
  • Configure Request includes a method for creating a processing pipeline applications.

When the application starts, it will call the ASP.NET Core runtime  ConfigureServices and  Configure:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
 
    public IConfiguration Configuration { get; }
 
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
    }
 
    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();
        });
    }
}

When building the application host specified  Startup class. Typically specified by calling WebHostBuilderExtensions.UseStartup <TStartup> method on the host builder  Startup classes:

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

Host provides  Startup class constructor available for certain services. Application by  ConfigureServices adding other services. Host and application services are in  Configure use and throughout the application.

When using generic host (IHostBuilder), type the following services can only be injected into the  Startup constructor:

  • IWebHostEnvironment
  • IHostEnvironment
  • IConfiguration
    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>();
                });
    }

    ConfigureServices method

    ConfigureServices method:

    • Optional.
    • In the  Configure prior method of configuring application services invoked by the host.
    • Which according to General Settings configuration options .

    Hosts might invoke  Startup configure some services before the method. For more information, please refer to the host .

    For the function requires a lot of settings, IServiceCollection  have the  Add{Service} extension method. For example, the Add DbContext, the Add DefaultIdentity, the Add EntityFrameworkStores and  the Add RazorPages:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
     
        public IConfiguration Configuration { get; }
     
        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();
        }

    Add services to the service container, and so the application of  Configure available methods. Service to parse through dependency injection or ApplicationServices.

    Configure Methods

    Configure a method for a given application in response to HTTP requests. By adding a middleware component configured to request pipeline IApplicationBuilder instance. Configure The method can be used  IApplicationBuilder, but not registered in the service container. Hosted create  IApplicationBuilder and pass it directly to  Configure.

    ASP.NET Core pipeline configuration template support:

    • Developers abnormal page
    • Exception handler
    • HTTP Strict Transport Security (HSTS)
    • HTTPS redirection
    • Static files
    • ASP.NET Core MVC 和 Razor Pages
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
       
          public IConfiguration Configuration { get; }
       
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddRazorPages();
          }
       
          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();
              });
          }
      }

      Each  Use extension method to add one or more requests to the middleware component conduit. For example, UseStaticFiles configure the middleware static files.

      Each middleware component request pipeline is responsible for calling the next pipeline component, or to a short circuit in the chain where appropriate.

      You can  Configure specify a different service method signature, as  IWebHostEnvironment, ILoggerFactory or  ConfigureServices as defined in any content. If these services are available, it will be injected.

      For information about how to use  IApplicationBuilder the detailed information and middleware processing order, see  ASP.NET Core middleware .

Guess you like

Origin www.cnblogs.com/puzi0315/p/12571800.html