Skip to content

MS_DotNetCoreConfig

nishi_74322014 edited this page Aug 12, 2026 · 1 revision

.NET Core config

概要

.NET Coreでは、
app.config、web.configの一部が、
appsettings.json に変更されている。

  • app.config の appSettings Section を JSON 化したもの。

  • 環境毎に設定を切り替えることができる。

    • appsettings.Production.json
      ASPNETCORE_ENVIRONMENT ≠ Development
    • appsettings.Development.json
      ASPNETCORE_ENVIRONMENT = Development
  • .NET configにあった、*.config

    • Element、Collection、Section 自体は無くなっているので、
      • ConfigurationElement
      • ConfigurationElementCollection
      • ConfigurationSection
    • 自身で設定ファイル(appsettings.json)等の
      • フォーマット定義
      • Startup.cs(vb) 実装

    が必要になる。

  • 一部、例外的に、.NET CoreIISでホストする場合、
    system.webServer Section Group にIIS.NET Core
    関する設定をするために、web.configを使用するケースがある。

補足(最新化:構成の「階層」): appsettings.json は
「唯一の構成ファイル」ではなく、構成プロバイダの積み重ねの 1 つに過ぎない。
既定のホスト(Host.CreateDefaultBuilder / WebApplication.CreateBuilder)では、
概ね以下の順に読み込まれ、後勝ちで上書きされる。

  1. appsettings.json
  2. appsettings.{Environment}.json
  3. ユーザー シークレット(開発環境のみ)
  4. 環境変数
  5. コマンドライン引数

したがって、本番の接続文字列やシークレットは
appsettings.Production.json に書かず、環境変数や
Azure Key Vault / Azure App Configuration などの
構成プロバイダから供給するのが定石である。

補足(環境変数名): ASPNETCORE_ENVIRONMENT は ASP.NET Core 用で、
Web ではない汎用ホスト(コンソール、ワーカー サービス)では
DOTNET_ENVIRONMENT が使用される。
値は Development / Staging / Production が既定の 3 種だが、
任意の文字列を定義することもできる。

詳細

以下の様に、API が変更になっている。

System.Configuration.ConfigurationManager

  • 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)と
    統合されないため、
    構成の変更検知やスコープ管理の恩恵を受けられない。

つまり「使えない」というよりは
「互換のために残されているが、新規実装で使うべきものではない」
と理解するのが正確である。

Microsoft.Extensions.Configuration

以下の情報を元にマイグレーションする必要がある。

補足(対応関係):

.NET Framework .NET Core 以降
ConfigurationManager.AppSettings["key"] configuration["key"]
ConfigurationManager.ConnectionStrings["名前"].ConnectionString configuration.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

ASP.NET Core における DI(MS_ASPNETCoreDI.md)を参照。

ASP.NET Coreのデータ保護

ASP.NET Coreのデータ保護(MS_ASPNETCoreDataProtection.md)を参照。

ASP.NET CoreのSession利用方法

ASP.NET CoreのSession利用方法(MS_ASPNETCoreSession.md)を参照。

参考

Qiita

microsoft.com

stackoverflow.com


Tags: 移行, .NET開発, .NET Core, 構成

NetDevInfraWiki

マイクロソフト系技術情報 Wiki
Open 棟梁 Wiki

(未着手)

開発基盤部会 Wiki

移行管理: DONETODO

Clone this wiki locally