亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET Core 中的 startup類

發布時間:2020-09-01 15:34:17 來源:網絡 閱讀:1556 作者:VOLVO之悅 欄目:編程語言

原文地址: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<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類在不同的環境(Environment),并在運行時選擇適當的一個啟動,如果在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 environmentsUseStartup<TStartup> 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<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>();
}


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

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新干县| 吉木乃县| 茂名市| 鄂托克旗| 淳安县| 黄大仙区| 定安县| 瓮安县| 郧西县| 西宁市| 沛县| 五指山市| 松阳县| 万宁市| 澄城县| 河北区| 曲阳县| 西乌珠穆沁旗| 兴文县| 思南县| 哈巴河县| 天水市| 靖远县| 南充市| 南召县| 龙江县| 通化县| 武山县| 电白县| 兴城市| 榆中县| 镇平县| 彰化县| 龙川县| 阿拉尔市| 平利县| 大安市| 和硕县| 诏安县| 南木林县| 元氏县|