[英]How to set environment variables or inputs in timerTrigger Azure Functions?
我正在嘗試設置一個timerTrigger
天藍色功能
我的function.json
:
{
"disabled": false,
"bindings": [
{
"type": "timerTrigger",
"direction": "in",
"name": "sampleCronTrigger",
"schedule": "*/5 * * * * *",
}
],
"entryPoint": "sampleCron",
"scriptFile": "index.js"
}
在這種情況下,我需要設置一個環境變量,但是我不能這樣做。 我嘗試查找一些文檔,但是找不到不需要在Azure控制台上進行某些設置的任何內容?
我可以定義環境變量嗎? 或者,如果我可以通過任何方式將輸入傳遞給函數,那也可以。
功能應用程序中的應用程序設置包含全局配置選項,這些選項會影響該功能應用程序的所有功能。 在本地運行時,這些設置將作為本地環境變量進行訪問。
本地設置文件文件local.settings.json
存儲應用程序設置,連接字符串和Azure Functions核心工具的設置。 僅在本地運行時,Functions工具才能使用local.settings.json
文件中的設置。 默認情況下,將項目發布到Azure時不會自動遷移這些設置。 發布時使用--publish-local-settings
開關以確保將這些設置添加到Azure中的功能應用程序。
在“功能”中,應用設置(例如服務連接字符串)在執行期間會作為環境變量公開。 您可以使用process.env訪問這些設置,如GetEnvironmentVariable函數中所示:
module.exports = function (context, myTimer) {
var timeStamp = new Date().toISOString();
context.log('Node.js timer trigger function ran!', timeStamp);
context.log(GetEnvironmentVariable("AzureWebJobsStorage"));
context.log(GetEnvironmentVariable("WEBSITE_SITE_NAME"));
context.done();
};
function GetEnvironmentVariable(name)
{
return name + ": " + process.env[name];
}
您可以通過多種方式添加,更新和刪除功能應用程序設置:
在本地運行時,將從local.settings.json項目文件中讀取應用程序設置。
參考文獻:
另外,要從local.settings.json中檢索值,另一種方法是使用ExecutionContext executionContext
創建IConfigurationRoot對象。
可以將ExecutionContext添加到函數定義中:
[FunctionName("FunctionName")]
public static async Task Run(
[ServiceBusTrigger(...)]
SomeMessage msg,
ILogger log,
ExecutionContext executionContext)
{
}
之后,您可以實例化一個IConfigurationRoot實例,該實例指示您有選擇地加載local.appsettings.json。
var configurationRoot = new ConfigurationBuilder()
.SetBasePath(executionContext.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
使用configurationRoot對象,您可以檢索配置值:
var value = configurationRoot["SomeKey"];
示例local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "...",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"SomeKey": "Value",
},
"Host": {
"LocalHttpPort": "7071"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.