LoginSignup
4
2

More than 1 year has passed since last update.

Blazor Serverでappsettings.jsonの値を取る。Azure App Serviceに発行したときはAzure上で設定した構成→アプリケーション設定で設定した値を取る。

Last updated at Posted at 2022-12-21

タイトルの内容を試行錯誤中(´・ω・`)
.NET5以前の情報もいっぱい引っ掛かって何も分からん状態…
appsettings.jsonじゃなくてweb.configから取るべきなのか…?

何とかできたっぽい。

参考にしたページ

環境

  • .NET7
  • Visual Studio 2022

appsettings.jsonの値を取る。

ケース①:IConfigurationから取る

何故かビルド通らなくなった… GetConnectionStringが未定義なら逆に何故今までビルドが通っていた…?
(エラーメッセージ)
'IConfiguration' に 'GetConnectionString' の定義が含まれておらず、最も適している拡張メソッド オーバーロード 'ConfigurationExtensions.GetConnectionString(IConfiguration, string)' には 'IConfiguration' 型のレシーバーが必要です
→解決したので別エントリに
Blazorで何故かIConfiguration(ConfigurationManager)のDIができない

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "$comment": "↓追加したヤツ",
  "ConnectionStrings": {
    "db_con": "(接続文字列)"
  }
}
ConnectionStrings.cs
namespace BlazorServerTestWeb.Utility {
  public class ConnectionStrings {
    public string db_con { get; set; } = "";
  }
}
Index.razor
@page "/"
@using BlazorServerTestWeb.Utility
@inject IConfiguration config;

db_con = @connection_str.db_con

@code {
    private ConnectionStrings connection_str = new ();

    protected override void OnInitialized() {
        config.GetSection(nameof(ConnectionStrings)).Bind(connection_str);
        //今回みたいにConnectionStringsならGetConnectionStringが使える。
        //connection_str.db_con = config.GetConnectionString("db_con");
    }
}

ケース②:IOptions<TOptions>経由で取る

Program.cs
//(前略)
builder.Services.Configure<ConnectionStrings>(
  builder.Configuration.GetSection(nameof(ConnectionStrings))
);

var app = builder.Build();
//(後略)
Index.razor
@page "/"
@using BlazorServerTestWeb.Utility
@using Microsoft.Extensions.Options
@inject IOptions<ConnectionStrings> _options;

db_con = @connection_str.db_con

@code {
    private ConnectionStrings connection_str = new ();

    protected override void OnInitialized() {
        connection_str = _options.Value;
    }
}

Azureに発行してみたけど動かん…何故…? →ちょっと待ったら動いた

image.png

でもApp Service側で設定した接続文字列が取れない…どうやったらいいんだろうか…? → できた!

NugetでSystem.Configuration.ConfigurationManager入れて使ってみる
→だめぽ

接続文字列じゃ無くてアプリケーション設定の方ならちゃんと取れてる。
どこがどうだめなんやろうか・・・(´・ω・`)

→これだ。
AppServiceの接続文字列に騙されるな!

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "$comment": "↓追加したヤツ",
  "db_con": "(appsettings.json の db_con)",
  "POSTGRESQLCONNSTR_db_con": "(appsettings.json の POSTGRESQLCONNSTR_db_con)",
  "ConnectionStrings": {
    "db_con": "(appsettings.json の ConnectionStrings の db_con)"
  }
}
Index.razor
@page "/IConfiguration"
@using Microsoft.Extensions.Configuration
@inject IConfiguration config;

config.GetConnectionString("db_con") = @connection_str1<br/>
config["ConnectionStrings:db_con"] = @connection_str2<br />
config["db_con"] = @connection_str3<br />
config["POSTGRESQLCONNSTR_db_con"] = @connection_str4<br />

@code {
    private string? connection_str1;
    private string? connection_str2;
    private string? connection_str3;
    private string? connection_str4;

    protected override void OnInitialized() {
        connection_str1 = config.GetConnectionString("db_con");
        connection_str2 = config["ConnectionStrings:db_con"];
        connection_str3 = config["db_con"];
        connection_str4 = config["POSTGRESQLCONNSTR_db_con"];
    }
}
  • App Service設定
    image.png

  • ローカル実行結果
    config.GetConnectionString("db_con") = (appsettings.json の ConnectionStrings の db_con)
    config["ConnectionStrings:db_con"] = (appsettings.json の ConnectionStrings の db_con)
    config["db_con"] = (appsettings.json の db_con)
    config["POSTGRESQLCONNSTR_db_con"] = (appsettings.json の POSTGRESQLCONNSTR_db_con)

  • App Service実行結果
    config.GetConnectionString("db_con") = (appsettings.json の ConnectionStrings の db_con)
    config["ConnectionStrings:db_con"] = (appsettings.json の ConnectionStrings の db_con)
    config["db_con"] = App Serviceのアプリケーション設定で設定したdb_con
    config["POSTGRESQLCONNSTR_db_con"] = App Serviceの接続文字列で設定したdb_con

結論:BlazorはASP.NETじゃないので「接続文字列」じゃなくて「アプリケーション設定」を使お

そうしよう。

4
2
1

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