[英]Azure Functions Environment.GetEnvironmentVariable returns null
[英]Azure Function Environment.GetEnvironmentVariable object reference error in Azure portal
我已使用Visual Studio 2017將應用程序發布到Azure Function。由於來自Environment變量的連接字符串始終為null,因此在日志中出現“對象引用”錯誤,但是,該應用程序在本地運行良好。 下面是我獲取連接字符串並記錄它的代碼段。
var helper = new Helper();
log.LogInformation($ "The connection string is {helper.GetConnectionString()}");
if (string.IsNullOrWhiteSpace(helper.GetConnectionString())) return req.CreateErrorResponse(HttpStatusCode.BadRequest, "Connection is null");
var signalRService = new SignalRService(helper.GetConnectionString(), helper.GetIotHubName(), log);
log.LogInformation("SignalRService has been initialized");
await signalRService.SendRequest(helper.GetIotHubName(), data?.ToString());
log.LogInformation("SignalRService has been invoked successfully");
return req.CreateResponse(HttpStatusCode.OK, "Success");
以下是我的助手班
public class Helper {
private static readonly string connectionStringName = "AzureSignalRConnectionString";
private static readonly string iotHubName = "iotHubName";
public string GetConnectionString() {
return Environment.GetEnvironmentVariable(connectionStringName, EnvironmentVariableTarget.Process);
}
public string GetIotHubName() {
return Environment.GetEnvironmentVariable(iotHubName, EnvironmentVariableTarget.Process);
}
}
當我在門戶中監視我的功能時,我可以清楚地看到連接字符串為null。 我已經在local.settings.json中給出了連接字符串。
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"iotHubName": "iot-hub",
"AzureSignalRConnectionString": "connection string value",
"MSDEPLOY_RENAME_LOCKED_FILES": 1
},
"Host": {
"LocalHttpPort": 5181,
"CORS": "*"
}
}
我不確定我在這里缺少什么。 任何幫助都非常感謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.