目的
Azure Functions をローカル環境でステップ実行したい。
言語ワーカー上での関数アプリコード実行を経て、Functions Host 上でどのように動作しているのか確認したり、独自の OutputBinding を作ったりするときに役立つと思います。
免責
この記事は単なる動かしてみたメモであり、オフィシャルな方法ではありません。
VS Code でのデバッグ実行方法
launch.json
すべてはここに書いてある
https://github.com/Azure/azure-functions-host/blob/dev/.vscode/launch.json
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/WebJobs.Script.WebHost/bin/Debug/net6.0/Microsoft.Azure.WebJobs.Script.WebHost.dll",
"args": [],
"cwd": "${workspaceFolder}/sample/Node",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"AZURE_FUNCTIONS_ENVIRONMENT": "Development",
"AzureWebJobsSecretStorageType": "Files"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
デバッグ実行するとどうなるか
この状態で、Functions Host のメインコードに対してブレークポイントが有効な状態で起動できる。
関数コードはどこから読み込まれるか
"cwd": "${workspaceFolder}/sample/Node",
に記載のディレクトリつまり、リポジトリ内の sample/Nodeをアプリケーションコードとしてロードして、Functions Host が起動する。
ただし、デフォルトでは、以下のようにバインディング拡張が読み込まれず、単純な HTTP トリガー関数しか動作確認ができない模様です。
自分の関数コードを利用したい
その場合は、cwd
にアプリケーションコードの host.json
があるディレクトリを指定します。
手っ取り早いのは、sample/Node
と同じ階層に作っちゃって sample/MyApp
とするのがよいかと思います。
AppSettingsを指定したい
そのままではlocal.settings.json は読み込まれないようです。
launch.json
の env
に記載しておけば読み込まれました。
extensionBundle がロードされない
通常、host.json に extensionBundle
を指定しておくことで、.NET in-process 以外の言語を利用する場合に拡張機能が利用できるようになりますが、指定してもうまくロードしてくれませんでした。
代替案として、アプリコードディレクトリ内で func extensions install --package
を実行し、.csproj ファイルを作成しておく必要がありました。
参考資料