我试图在本地测试Azure函数时读取环境变量(使用v1.NET4.7.1)
[FunctionName( "test" )]
public static async Task<HttpResponseMessage> Run( [HttpTrigger( AuthorizationLevel.Anonymous, "post", Route = null )]HttpRequestMessage req, TraceWriter log )
{
String s;
log.Info( "test: Begin..." );
s = System.Environment.GetEnvironmentVariable( "test" );
if ( String.IsNullOrEmpty( s ) )
{
log.Info( "Unable to get environment key !!!!" );
}
else
{
log.Info( "Key value = " + s );
}
}
这个local.settings.json文件文件包含:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"test": "test_value"
}
}
函数编译并运行,但从不返回环境键值,日志始终包含:
Unable to get environment key !!!!
更新:2018年9月9日
-特别感谢Karishma Tiwari,他指引了我正确的方向:
问题是(正如karisha指出的)local.settings.json文件未将文件复制到输出路径,通过比较不工作的项目(左侧)和工作的项目(右侧)中的设置,如下所示:
解决方法:
-
-
将“Copy to Output Directory”设置更改为:“Copy if newer”
另一点需要注意的是
MS documentation states
"... 但我们建议您使用GetEnvironmentVariable,