-
Notifications
You must be signed in to change notification settings - Fork 0
MS_DotNetCoreConfig
- 戻る(.NET config、.NETのクロスプラットフォーム対応)
- .NET Core config
- .NET Coreへの移行 / .NET Standardへの移行
.NET Coreでは、
app.config、web.configの一部が、
appsettings.json に変更されている。
-
app.config の
appSettingsSection を JSON 化したもの。 -
環境毎に設定を切り替えることができる。
- appsettings.Production.json
ASPNETCORE_ENVIRONMENT≠ Development - appsettings.Development.json
ASPNETCORE_ENVIRONMENT= Development
- appsettings.Production.json
-
.NET configにあった、
*.configの- Element、Collection、Section 自体は無くなっているので、
ConfigurationElementConfigurationElementCollectionConfigurationSection
- 自身で設定ファイル(appsettings.json)等の
- フォーマット定義
-
Startup.cs(vb) 実装
が必要になる。
- Element、Collection、Section 自体は無くなっているので、
-
一部、例外的に、.NET CoreをIISでホストする場合、
system.webServerSection Group にIISや.NET Coreに
関する設定をするために、web.configを使用するケースがある。- 参考
ASP.NET Core アプリケーションでも web.config をカスタマイズしたい - しばやん雑記
https://blog.shibayan.jp/entry/20190604/1559616472
- 参考
補足(最新化:構成の「階層」): appsettings.json は
「唯一の構成ファイル」ではなく、構成プロバイダの積み重ねの 1 つに過ぎない。
既定のホスト(Host.CreateDefaultBuilder/WebApplication.CreateBuilder)では、
概ね以下の順に読み込まれ、後勝ちで上書きされる。
- appsettings.json
- appsettings.
{Environment}.json- ユーザー シークレット(開発環境のみ)
- 環境変数
- コマンドライン引数
したがって、本番の接続文字列やシークレットは
appsettings.Production.json に書かず、環境変数や
Azure Key Vault / Azure App Configuration などの
構成プロバイダから供給するのが定石である。
補足(環境変数名):
ASPNETCORE_ENVIRONMENTは ASP.NET Core 用で、
Web ではない汎用ホスト(コンソール、ワーカー サービス)では
DOTNET_ENVIRONMENTが使用される。
値はDevelopment/Staging/Productionが既定の 3 種だが、
任意の文字列を定義することもできる。
以下の様に、API が変更になっている。
- NuGet パッケージにあるが使えない(詳細不明)。
- なので、
Microsoft.Extensions.Configurationを使用する。
移行メモ(正誤):
System.Configuration.ConfigurationManagerの
NuGet パッケージ自体は .NET Core / .NET 5 以降でも動作する。
ただし、
- 読み込まれるのは(実行可能形式に対応する)
*.exe.configであり、
ASP.NET Core が読む appsettings.json ではない。ConfigurationSectionを使ったカスタム セクションは
Windows 以外では期待どおりに動作しないことがある。- ASP.NET Core のホスティング(
IConfiguration/ オプション パターン / DI)と
統合されないため、
構成の変更検知やスコープ管理の恩恵を受けられない。つまり「使えない」というよりは
「互換のために残されているが、新規実装で使うべきものではない」
と理解するのが正確である。
以下の情報を元にマイグレーションする必要がある。
補足(対応関係):
.NET Framework .NET Core 以降 ConfigurationManager.AppSettings["key"]configuration["key"]ConfigurationManager.ConnectionStrings["名前"].ConnectionStringconfiguration.GetConnectionString("名前")ConfigurationSectionを継承した独自クラスPOCO クラス + Bind/Configure<T>(オプション パターン)configSourceによる分割AddJsonFileの追加呼び出し設定変更に伴う AppDomain 再起動 reloadOnChange: true+IOptionsMonitor<T>オプション パターンの例。
// appsettings.json // { "MyOptions": { "Timeout": 30, "Endpoint": "https://example.com/" } } public class MyOptions { public int Timeout { get; set; } public string Endpoint { get; set; } } // Program.cs builder.Services.Configure<MyOptions>( builder.Configuration.GetSection("MyOptions")); // 利用側(コンストラクタ インジェクション) public class MyService { private readonly MyOptions _options; public MyService(IOptions<MyOptions> options) => _options = options.Value; }なお、環境変数から階層構造を指定する場合の区切りは
:だが、
Linux では:が使えないため__(アンダースコア 2 つ)を使用する
(例:MyOptions__Timeout=30)。
ASP.NET Core における DI(MS_ASPNETCoreDI.md)を参照。
ASP.NET Coreのデータ保護(MS_ASPNETCoreDataProtection.md)を参照。
ASP.NET CoreのSession利用方法(MS_ASPNETCoreSession.md)を参照。
- ASP.NET Coreの環境を切り替える方法 - athome-developer's blog
http://dblog.athome.co.jp/entry/2016/08/03/113000
- Visual Studio 2017 による ASP.NET Core MVC 開発
https://qiita.com/hiromasa-masuda/items/5b9ff175aac3d1ef6ca6 - ASP.NET Core MVC における構成ファイル appsettings.json からの値取得
https://qiita.com/hiromasa-masuda/items/d7e33b20d3eedee771f4
- ASP.NET Core の構成 | Microsoft Learn
https://learn.microsoft.com/ja-jp/aspnet/core/fundamentals/configuration/ - .NET の構成 | Microsoft Learn
https://learn.microsoft.com/ja-jp/dotnet/core/extensions/configuration - .NET のオプション パターン | Microsoft Learn
https://learn.microsoft.com/ja-jp/dotnet/core/extensions/options
- ASP.NET Core configuration for .NET Core console application - Stack Overflow
https://stackoverflow.com/questions/38114761/asp-net-core-configuration-for-net-core-console-application - c# - How to SetBasePath in ConfigurationBuilder in Core 2.0 - Stack Overflow
https://stackoverflow.com/questions/46843367/how-to-setbasepath-in-configurationbuilder-in-core-2-0
Tags: 移行, .NET開発, .NET Core, 構成
このWikiは「Open棟梁Project」,「OSSコンソーシアム 開発基盤部会」によって運営されています。