7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Application Startup in ASP.NET Core - 日本語意訳

Last updated at Posted at 2017-03-15

ASP.NET Core でのアプリケーションの起動

Startup クラスは、アプリケーションに対して行われたすべてのリクエストを処理するリクエストパイプラインを構成します。

Startup クラス

ASP.NET Core アプリケーションには Startup クラスが必要です。慣例として、Startup クラスの名前は "Startup" です。Main メソッド内で、WebHostBuilderExtensions クラスの
UseStartup<TStartup> メソッドに Startup クラス名を指定します。

異なる環境に応じて別々の Startup クラスを定義することができ、実行時に適切なクラスが選択されます。

WebHost の構成またはオプションの中で startupAssembly を指定すると、ホストはその起動アセンブリを読み込み、Startup または Startup[Environment] タイプを検索します。

StartupLoader クラスの FindStartupType メソッドと Working with multiple environments を参照してください。 UseStartup<TStartup> メソッドは推奨されるアプローチです。

Startup クラスのコンストラクタに、dependency injection によってオブジェクトを注入することができます。例えば、IHostingEnvironment を注入して構成ソースを設定したり、ILoggerFactory を注入してlogging プロバイダーを設定することができます。

Startup クラスには Configure メソッドが必須で、オプションで ConfigureServices メソッドを含めることができます。どちらもアプリケーションの起動時に呼び出されます。Startup クラスはまた、これらのメソッドの環境固有バージョンを含めることもできます。

handling exceptions during application startup について学びましょう.

Configure メソッド

Configure メソッドは、ASP.NET Core アプリケーションが HTTP リクエストにどのように応答するかを指定するために使用されます。リクエストパイプラインは、middleware コンポーネントを IApplicationBuilder インスタンスに追加することによって構成されます。IApplicationBuilder インスタンスは Dependency Injection によって注入されます。

以下のデフォルト Web サイトテンプレートのサンプルでは、いくつかの拡張メソッドを使用して、BrowserLink、error pages、 static files、 ASP.NET MVC、 および Identity をサポートするパイプラインを構成しています。

C#
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?}");
    });
}

Use 拡張メソッドは、リクエストパイプラインに Middleware コンポーネントを追加します。例えば、UseMvc 拡張メソッドは Routing Middleware をリクエストパイプラインに追加し、MVC をデフォルトハンドラーとして構成します。

IApplicationBuilder の使い方の詳細については、Middlewareを参照してください。

IHostingEnvironmentILoggerFactory のような追加の Service もメソッドの引数に指定することができます。これらの Service が利用可能であれば注入 されます。

ConfigureServices メソッド

ConfigureServices メソッドはオプションですが、もし使用された場合は、実行時に Configure メソッドの前に呼び出されます(一部の機能はリクエストパイプラインに組み込まれる前に追加されます)。 構成オプション はこのメソッドで設定されます。

実質的なセットアップが必要な機能のために、IServiceCollection には Add[Service] 拡張メソッドが用意されています。このデフォルト Web サイトテンプレートのサンプルでは、Entity Framework、Identity、および MVC の Service を使用するようにアプリケーションを構成しています。

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

IServiceCollection に 追加された Service は、
Dependency Injection を介してアプリケーション内で利用できるようになります。

Startup クラスで利用可能な Service

ASP.NET Core の Dependency Injection はアプリケーションの起動時にアプリケーションサービスを提供します。これらのサービスを使用するには、Startup クラスのコンストラクタまたは Configure メソッドまたは ConfigureServices メソッドの引数として適切なインターフェースを指定します。

Startup クラスの各メソッドを呼び出し順に見てみましょう、それぞれ以下のサービスが引数で指定できます。

  • コンストラクタ
  • IHostingEnvironment
  • ILoggerFactory
  • ConfigureServices メソッド
  • IServiceCollection
  • Configure メソッド
  • IApplicationBuilder
  • IHostingEnvironment
  • ILoggerFactory
  • IApplicationLifetime

その他のリソース

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?