我已使用visual studio 2017将我的应用程序发布到Azure函数。由于来自环境变量的连接字符串总是空的,我在日志中得到了对象引用错误,但是,应用程序在本地运行良好。下面是我获取连接字符串并记录它的代码片段。
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": "*"
}
}
我不确定我在这里错过了什么。非常感谢您的帮助。