重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ASP.NETCore中的startup类-创新互联

原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup

成都创新互联公司是一家集网站建设,平坝企业网站建设,平坝品牌网站建设,网站定制,平坝网站建设报价,网络营销,网络优化,平坝网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

下文:

--Startup类

--Configure方法

--ConfigureServices方法

--可启用的服务(Services Available in Startup)

--其他资源

Startup类配置请求管道,处理所有应用程序接收的请求

The Startup class configures the request pipeline that handles all requests made to the application.

Startup

asp.net core需要启动类,通常以“Startup”命名,在程序WebHostBuilderExtensions中的UseStartup中指定Startup类名。

ASP.NET Core apps require a Startup class. By convention, the Startup class is named "Startup". You specify the startup class name in the Main programs WebHostBuilderExtensions UseStartup method.

你能分别定义Startup类在不同的环境(Environment),并在运行时选择适当的一个启动,如果在WebHost的配置或操作中指定了startupAssembly程序集,托管将加载startup程序集并查找Startup 或 Startup(Environment)类型,参考 StartupLoader 的 FindStartupType 和 Working with multiple environments,建议使用UseStartup .

You can define separate Startup classes for different environments, and the appropriate one will be selected at runtime. If you specify startupAssembly in the WebHost configuration or options, hosting will load that startup assembly and search for a Startup or Startup[Environment] type. See FindStartupType in StartupLoader and Working with multiple environments. UseStartup is the recommended approach.

Startup类构造函数能接受通过依赖注入的依赖关系,你能用IHostingEnvironment设置配置,用ILoggerFactory设置logging提供程序。

The Startup class constructor can accept dependencies that are provided through dependency injection. You can use IHostingEnvironment to set up configuration sources and ILoggerFactory to set up logging providers.

Startup类必须包含 方法 而 方法可选,两个方法都在程序启动时调用,这个类也可包括这些方法的特定环境版本。

The Startup class must include a Configure method and can optionally include a ConfigureServices method, both of which are called when the application starts. The class can also include environment-specific versions of these methods.

了解关于在程序启动时的异常处理

Learn about handling exceptions during application startup.

Configure方法

configure方法用于指定ASP.NET程序如何响应HTTP请求,通过将中间件组件添加到由依赖注入提供的IApplicationBuilder实例中来配置请求管道。

The Configure method is used to specify how the ASP.NET application will respond to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance that is provided by dependency injection.

下面是来自默认网站模板的示例,对管道增加一些扩展方法用于支持  BrowserLink, error pages, static files, ASP.NET MVC, and Identity.

In the following example from the default web site template, several extension methods are used to configure the pipeline with support for BrowserLink, error pages, static files, ASP.NET MVC, and Identity

public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                         ILoggerFactory loggerFactory)
{
 
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));    
    loggerFactory.AddDebug();    
    
    if (env.IsDevelopment())
    {    app.UseDeveloperExceptionPage();
         app.UseDatabaseErrorPage();
         app.UseBrowserLink();
    }
    else{        
        app.UseExceptionHandler("/Home/Error");
    }    
    
    app.UseStaticFiles();    
    app.UseIdentity();    
    
    app.UseMvc(routes =>{
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

每个扩展方法添加一个中间件组件到请求管道中,例如,UseMvc扩展方法添加routing中间件到请求管道中并配置将MVC做为默认处理

Each Use extension method adds a middleware component to the request pipeline. For instance, the UseMvc extension method adds the routing middleware to the request pipeline and configures MVCas the default handler.

关于IApplicationBuilder的详细信息见 中间件。

For more information about how to use IApplicationBuilder, see Middleware.

也可以在方法签名中指定一些其他服务,如 IHostingEnvironment 和 ILoggerFactory,在这种情况下如果他们可用将被注入这些服务。

Additional services, like IHostingEnvironment and ILoggerFactory may also be specified in the method signature, in which case these services will be injected if they are available.

ConfigureServices方法

ConfigureServices方法是可选的,但若调用将在 Configure 之前被调用(一些功能会在链接到请求管道之前添加上),配置操作在方法中设置。

The ConfigureServices method is optional; but if used, it's called before the Configure method by the runtime (some features are added before they're wired up to the request pipeline). Configuration options are set in this method.

对于需要大量设置的功能,用  IServiceCollection的 Add[Service]扩展方法,下面是默认网站模板示例,将 Entity Framework, Identity, and MVC配置到程序中。

For features that require substantial setup there are Add[Service] extension methods on IServiceCollection. This example from the default web site template configures the app to use services for Entity Framework, Identity, and MVC.

public void ConfigureServices(IServiceCollection services)
{    
    // Add framework services.
    services.AddDbContext(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        
    services.AddIdentity()
        .AddEntityFrameworkStores()
        .AddDefaultTokenProviders();
        
    services.AddMvc();

    // Add application services.
    services.AddTransient();
    services.AddTransient();
}

向服务容器中添加服务,使他们可以通过依赖注入在你的程序中使用。

Adding services to the services container makes them available within your application via dependency injection.

Startup可用的服务

ASP.NET Core 依赖注入 是在 程序的startup中 提供程序服务,你能请求这些服务通过 Startup类的构造函数 或 它的Configure和 ConfigureServices 发放中 将适当的接口作为一个参数传入。

ASP.NET Core dependency injection provides application services during an application's startup. You can request these services by including the appropriate interface as a parameter on your Startup class's constructor or one of its Configure or ConfigureServices methods.

按调用顺序看 Startup类中的每个方法,服务作为一个参数被请求

Looking at each method in the Startup class in the order in which they are called, the following services may be requested as parameters:

  • In the constructor: IHostingEnvironmentILoggerFactory

  • In the ConfigureServices method: IServiceCollection

  • In the Configure method: IApplicationBuilderIHostingEnvironmentILoggerFactoryIApplicationLifetime

其他资源

  1. Working with Multiple Environments

  2. Middleware

  3. Logging

  4. Configuration

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


标题名称:ASP.NETCore中的startup类-创新互联
文章路径:http://cqcxhl.cn/article/ceggss.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP