LoginSignup
0
0

Azure Functions の local.settings.json なくしがち問題

Posted at

リポジトリからソースコードをクローンして Azure Functions をデバッグ実行してみたところ、ユーザーシークレットが取得できない。何もせずとも読み込まれるはずなのに。

ASP.NET Core シークレット マネージャーによって作成されたシークレットは、Azure Functions Core Tools (バージョン 3.0.3233 以降) によって自動的に読み取られます。

また、コンソールには怪しいメッセージ。

Can't determine project language from files. Please use one of [--csharp, --javascript, --typescript, --java, --python, --powershell, --custom]
Can't determine project language from files. Please use one of [--csharp, --javascript, --typescript, --java, --python, --powershell, --custom]
Can't determine project language from files. Please use one of [--csharp, --javascript, --typescript, --java, --python, --powershell, --custom]

いろいろ調べた結果、Visual Studio で Azure Functions プロジェクトを新規作成したときに生成される無視ファイルにこの行が。

.gitignore
# Azure Functions localsettings file
local.settings.json

プッシュしたときに local.settings.json が抜けてしまい、クローンした際になくしてしまったようです。
プロジェクトを新規作成したときに生成される local.settings.json は、以下。

local.settings.json
{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

なるほど、このファイルによってプロジェクトの言語を定義しているのですね。それがないので一番最初のメッセージが出ていたと。
local.settings.json をプロジェクトに追加することでエラーメッセージが消えてユーザーシークレットを読み込むことができるようになりました。このファイルに接続文字列やシークレットを書くことはないので、プッシュしてしまっていいでしょう。


ちなみに、試行錯誤の中で ConfigureAppConfiguration で明示的にユーザーシークレットを追加することで読み込むこともできました。

Startup.cs
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System.Reflection;

[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))]

namespace FunctionApp1

public sealed class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
    }

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        base.ConfigureAppConfiguration(builder);

        builder.ConfigurationBuilder
            .AddUserSecrets(Assembly.GetExecutingAssembly(), optional: true)
            .AddEnvironmentVariables()
            .Build();
    }
}

参考

0
0
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
0
0