我已使用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);
}
}
当我在门户中监视我的功能时,我可以清楚地看到连接字符串为空。我已经在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": "*"
}
}
我不确定这里缺少什么。任何帮助都非常感谢。