【问题标题】:Default Azure Function app base path D:\home\site\wwwroot Azure Function 2.x默认 Azure Function 应用程序基本路径 D:\home\site\wwwroot Azure Function 2.x
【发布时间】:2019-03-19 14:22:50
【问题描述】:

目前,默认情况下,Azure 函数基本路径设置为“D:\home\site\wwwroot”。例如,发布时,VS 将应用上传到此文件夹。

我需要从这个文件夹中读取配置文件。我们有ExecutionContext is null via dependency injection via constructor的问题

如果以后更改路径,设置新的环境变量可能会导致问题。

我的问题是如何使用可靠且稳定的应用程序基本路径,通过构造函数与 DI 一起使用。

Azure 函数 2.x

VS 2017

【问题讨论】:

    标签: azure azure-functions


    【解决方案1】:

    您可以使用 function.json 来获得配置密钥对。 例如:

    System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
    

    在function.json中你可以这样做: “mykey”:“myvalue”

    {
      "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.24",
      "configurationSource": "attributes",
      "bindings": [
      {
          "direction":"in",
          "type": "timerTrigger",
          "useMonitor": true,
          "runOnStartup": false,
          "name": "myTimer",
          "mykey": "myvalue"
      }
      ],
          "disabled": false,
          "scriptFile": "../bin/**.dll",
          "entryPoint": "**.Run"
    } 
    

    【讨论】:

    • 正如我在 OP 中提到的:如果将来更改路径,设置新的环境变量可能会导致问题。
    【解决方案2】:

    有一个环境变量指向home 目录。这不会改变包括功能应用程序在内的许多服务依赖于它。下面是函数运行时如何在 azure 环境中获取它。

        string home = GetEnvironmentVariable("HOME");
        Path = System.IO.Path.Combine(home, "site", "wwwroot");
    

    【讨论】:

    • 可以提供链接吗?
    • 如另一篇文章中所述。这就是 Azure 应用服务和 Azure 函数确定此路径的方式。它在所有托管环境中都是稳定且一致的。 Azure 函数上的 wiki 链接在此处部分记录了这一点:github.com/Azure/azure-functions-host/wiki/…
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多