1
2

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.

ASP .Net Core3.1 起動プロファイルと設定ファイルについて

Last updated at Posted at 2020-04-05

はじめに

「.Net Core 3.1について調べてみよう」その2です。
Visual studio 2019でASP .Net Coreのプロジェクトを作成するとlaunchSettings.jsonとappsettings.jsonがプロジェクトに追加されています。これらのファイルの使い方が気になったので調べてみました。

launchSettings.json

Visual Studio等の開発環境で、アプリケーションを起動する際の環境とパラメータを指定するために使用されます。

launchSettings.json
{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:5002",
      "sslPort": 44314
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Weatherforecast": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5001"
    },
    "newProfile1": {
      "commandName": "Executable",
      "executablePath": "C:\\weatherforecast\\bin\\Debug\\netcoreapp3.1\\weatherforecast.exe"
    }
}

commnadNameによって以下のように分けられます。

commandName 起動方法
IISExpress IISExpress
IIS IIS
Project 実行ファイル
Executable 指定された実行ファイル

dotnet runで実行した場合は、Projectの設定で実行され、Projectの設定が複数ある場合、ファイルの最初に定義されているものが実行されます。

appsettings.json

.Net Frameworkのアプリケーション設定ファイル(exe.config)の代わりとなるファイルです。

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

このファイルは実行時のASPNETCORE_ENVIRONMENT環境変数によって切り替えることが可能です。
appsettings.ASPNETCORE_ENVIRONMENT.jsonファイルがあれば、appsettings.jsonの代わりに使用されます。
例えば、ASPNETCORE_ENVIRONMENT=Developmentの場合、
appsettings.Development.jsonファイルがあれば、このファイルが使用されます。

appsettings.json
{
  "test":{
    "abc":1
  }
}

追加した"test"をコードから読み込みます。

WeatherForecast.cs
private readonly IConfiguration configuration_;

public WeatherForecastController(IConfiguration configuration)
{
    configuration_ = configuration;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
    //値を読み込む
    var v = configuration_.GetValue<int>("test:abc");    
}

また、直接アクセスせずにクラスにアサインすることもできます。

Test.cs
public class Test
{
    public int abc { get; set; }
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.Configure<Test>(Configuration.GetSection("test"));
}
WeatherForecast.cs
private readonly IOptions<Test> testConfiguration_;

public WeatherForecastController(IOptions<Test> testConfiguration)
{
    testConfiguration_ = testConfiguration;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
    //値を読み込む
    var v = testConfiguration_.Value.abc;    
}

最後に

launchSettings.jsonは、publish(発行)には含まれないので開発専用の設定ですね。
Kestrelを使用する場合、運用時の設定は、appsettings.Production.jsonに行い、Startup.csでこの設定をKestrelServerOptionsクラスにアサインすればいいようです。
この設定は、json:launchSettings.jsonのapplicationUrlよりも優先されます。そのため、appsettings.jsonに定義してしまうとapplicationUrlの設定を上書きしてしまいます。

appsettings.Production.json
{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:8080"
      }
    }
  }
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));
}

appsettings.jsonの設定を読み取る際に、IConfigrationのGetValue<type>("key")を使用すると、そのたびにファイルアクセスが発生するという記事を見かけたので調べてみましたが、アプリケーション起動時に1度だけ読み込んこまれてメモリ上に保存されていました。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?