はじめに
「.Net Core 3.1について調べてみよう」その2です。
Visual studio 2019でASP .Net Coreのプロジェクトを作成するとlaunchSettings.jsonとappsettings.jsonがプロジェクトに追加されています。これらのファイルの使い方が気になったので調べてみました。
launchSettings.json
Visual Studio等の開発環境で、アプリケーションを起動する際の環境とパラメータを指定するために使用されます。
{
"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)の代わりとなるファイルです。
{
"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ファイルがあれば、このファイルが使用されます。
{
"test":{
"abc":1
}
}
追加した"test"をコードから読み込みます。
private readonly IConfiguration configuration_;
public WeatherForecastController(IConfiguration configuration)
{
configuration_ = configuration;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
//値を読み込む
var v = configuration_.GetValue<int>("test:abc");
}
また、直接アクセスせずにクラスにアサインすることもできます。
public class Test
{
public int abc { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.Configure<Test>(Configuration.GetSection("test"));
}
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の設定を上書きしてしまいます。
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:8080"
}
}
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));
}
appsettings.jsonの設定を読み取る際に、IConfigrationのGetValue<type>("key")
を使用すると、そのたびにファイルアクセスが発生するという記事を見かけたので調べてみましたが、アプリケーション起動時に1度だけ読み込んこまれてメモリ上に保存されていました。