c# - Azure 门户中的 Azure Function Environment.GetEnvironmentVariable 对象引用错误
问题描述
我已使用 Visual Studio 2017 将我的应用程序发布到 Azure Function。由于环境变量中的连接字符串始终为空,我在日志中收到对象引用错误,但是,该应用程序在本地运行良好。下面是我获取连接字符串并记录它的一段代码。
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);
}
}
当我在门户中监视我的功能时,我可以清楚地看到连接字符串为空。我已经在 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": "*"
}
}
我不确定我在这里缺少什么。非常感谢任何帮助。
解决方案
推荐阅读
- isabelle - 如何为存在量词生成代码
- react-native - Fusion Chart (bar2d) 无法在 React Native 中滚动
- java - 如何在不从本地 xml、csv 文件访问 API 的情况下将变量发布到 php 站点?
- javascript - 将鼠标悬停在上下文菜单上时如何更改上下文菜单的 li 项的颜色
- javascript - 与索引 0 的 Levenshtein 距离
- php - 将“Image”标签替换为“a”标签 PHP DOMDocument
- azure-storage - 如何检查Azure容器中是否存在多个文件
- nginx - Nginx 服务于一个子域的根而不是另一个子域的根
- console - 如果qpad关闭但端口打开,qpad查询可以恢复吗?
- css - 媒体查询纵横比