【问题标题】:Azure Function - The ConnectionString property has not been initializedAzure 函数 - ConnectionString 属性尚未初始化
【发布时间】:2019-03-30 03:55:46
【问题描述】:

我在 Visual Studio 中开发了一个 Azure 函数,它在 Azure 中发布时可以工作(从一年前开始)。

现在我在 Visual Studio 中使用该 Azure 函数制作了一个模板,并更改了一些细节,但基本相同。当我在本地测试它时,它工作正常。

但是当我在 Azure 中发布它并尝试对其进行测试时,我得到了这个错误:

The ConnectionString property has not been initialized

我通常在应用程序设置中编写连接字符串(它适用于较旧的 Azure 功能)。

以下是函数获取连接字符串值的方式:

var repo = new GranularRepository(ConfigurationManager.AppSettings["BoConnectionString"]);

我也试过了:

var repo = new AvgDeliveryTime_GranularRepository(Environment.GetEnvironmentVariable("BodbConnectionString"));

【问题讨论】:

  • 在门户中浏览到应用程序时是否看到此设置的值?
  • 查看此线程并在 6 月 25 日从 johnwc 发表评论。 github.com/Azure/Azure-Functions/issues/717
  • 您在哪个版本的 Functions 运行时遇到此问题,v1 还是 v2?

标签: azure connection-string azure-functions


【解决方案1】:

根据你的描述,我无法区分你的函数运行时。

对于 v1:

您可以同时使用 ConfigurationManager.AppSettingsSystem.Environment.GetEnvironmentVariable 在 Azure 中获取连接字符串。

var a = ConfigurationManager.AppSettings["BoConnectionString"];
var b = System.Environment.GetEnvironmentVariable("BoConnectionString");

对于 v2:

您可以使用System.Environment.GetEnvironmentVariableConfigurationBuilder 来获取它。添加ExecutionContext参数,用于定位函数应用目录。

var a= System.Environment.GetEnvironmentVariable("BoConnectionString");
/////////////
public static void Run(...,ExecutionContext context)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

    // Get Connection strings
    var connParameter= "MySqlAzureConnection";
    string connectionString = config.GetConnectionString($"{connParameter}");
}

v1 和 v2 都在应用程序设置中设置了连接字符串。

更多详情可以参考这个issue

【讨论】:

    【解决方案2】:

    我通过创建一个新的 Azure Function 项目来修复它。

    所以我的结论是,从另一个 Azure 函数制作 模板 并将其用作 Visual Studio 中的新项目会产生问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多