成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

ASP.NETCore中的startup類

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

巴彥網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,巴彥網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為巴彥千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的巴彥做網(wǎng)站的公司定做!

下文:

--Startup類

--Configure方法

--ConfigureServices方法

--可啟用的服務(wù)(Services Available in Startup)

--其他資源

Startup類配置請求管道,處理所有應(yīng)用程序接收的請求

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

Startup

asp.net core需要啟動類,通常以“Startup”命名,在程序WebHostBuilderExtensions中的UseStartup<TStartup>中指定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<TStartup> method.

你能分別定義Startup類在不同的環(huán)境(Environment),并在運行時選擇適當?shù)囊粋€啟動,如果在WebHost的配置或操作中指定了startupAssembly程序集,托管將加載startup程序集并查找Startup 或 Startup(Environment)類型,參考 StartupLoader 的 FindStartupType 和 Working with multiple environments,建議使用UseStartup<TStartup> .

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<TStartup> is the recommended approach.

Startup類構(gòu)造函數(shù)能接受通過依賴注入的依賴關(guān)系,你能用IHostingEnvironment設(shè)置配置,用ILoggerFactory設(shè)置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類必須包含 方法 而 方法可選,兩個方法都在程序啟動時調(diào)用,這個類也可包括這些方法的特定環(huán)境版本。

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.


了解關(guān)于在程序啟動時的異常處理

Learn about handling exceptions during application startup.


Configure方法

configure方法用于指定ASP.NET程序如何響應(yīng)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.

下面是來自默認網(wǎng)站模板的示例,對管道增加一些擴展方法用于支持  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.

關(guān)于IApplicationBuilder的詳細信息見 中間件。

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

也可以在方法簽名中指定一些其他服務(wù),如 IHostingEnvironment 和 ILoggerFactory,在這種情況下如果他們可用將被注入這些服務(wù)。

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方法是可選的,但若調(diào)用將在 Configure 之前被調(diào)用(一些功能會在鏈接到請求管道之前添加上),配置操作在方法中設(shè)置。

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.

對于需要大量設(shè)置的功能,用  IServiceCollection的 Add[Service]擴展方法,下面是默認網(wǎng)站模板示例,將 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<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
        
    services.AddMvc();

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

向服務(wù)容器中添加服務(wù),使他們可以通過依賴注入在你的程序中使用。

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


Startup可用的服務(wù)

ASP.NET Core 依賴注入 是在 程序的startup中 提供程序服務(wù),你能請求這些服務(wù)通過 Startup類的構(gòu)造函數(shù) 或 它的Configure和 ConfigureServices 發(fā)放中 將適當?shù)慕涌谧鳛橐粋€參數(shù)傳入。

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.

按調(diào)用順序看 Startup類中的每個方法,服務(wù)作為一個參數(shù)被請求

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

網(wǎng)頁名稱:ASP.NETCore中的startup類
轉(zhuǎn)載源于:http://jinyejixie.com/article6/gcshig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作App設(shè)計、Google自適應(yīng)網(wǎng)站、網(wǎng)站制作、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
南江县| 璧山县| 哈巴河县| 达孜县| 年辖:市辖区| 崇仁县| 班戈县| 土默特右旗| 高邑县| 博野县| 博野县| 专栏| 千阳县| 兴义市| 略阳县| 红原县| 福建省| 兰溪市| 化德县| 南宫市| 江津市| 虎林市| 安岳县| 沽源县| 遂昌县| 宣武区| 庆云县| 房产| 青田县| 新丰县| 冕宁县| 从江县| 乌审旗| 石城县| 宜兰县| 辽源市| 永定县| 卢龙县| 张家川| 江油市| 岳阳市|