簡體   English   中英

Azure門戶中的Azure Function Environment.GetEnvironmentVariable對象引用錯誤

[英]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": "*"
  }
}

我不確定我在這里缺少什么。 任何幫助都非常感謝。

我弄清楚了我在這里想念的東西。 當涉及到環境變量時,僅將它們添加到local.settings.json並沒有幫助。 當我登錄到Azure函數並檢查“應用程序”設置時,我找不到在local.settings.json中給出的設置。 我做了以下步驟。

  1. 在Visual Studio中打開“發布”設置
  2. 單擊“管理應用程序設置”,我缺少兩個環境變量的遠程字段中的值。 在此處輸入圖片說明
  3. 在遠程字段中添加值。
  4. 重建和發布功能應用
  5. 登錄到Azure門戶,並確保值在應用程序設置下存在。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM