Azure Function - The ConnectionString property has not been initialized
Asked Answered
A

3

5

I developed a Azure Function in Visual Studio and it works when published in Azure (since a year ago).

Now I made a template from that Azure Function, in Visual Studio, and I changed some details, but it's mainly the same. When I test it locally, it works fine.

But when I publish it in Azure and try to test it, I get this error:

The ConnectionString property has not been initialized

I usually write the Connection String in the Application Settings (and it works well for the older Azure functions).

Here is how the function gets the value of the Connection String:

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

I also tried:

var repo = new AvgDeliveryTime_GranularRepository(Environment.GetEnvironmentVariable("BodbConnectionString"));
Aleris answered 25/10, 2018 at 13:47 Comment(3)
Do you see the value for this setting when browse to the app in the portal?Praline
Check this thread and comment from johnwc on June25. github.com/Azure/Azure-Functions/issues/717Altimetry
Which version of the Functions runtime are you experiencing this issue in, v1 or v2?Acarology
A
1

I fixed it by creating a new Azure Function project.

So my conclusion is that making a template from another Azure Function and using that as the new project, in Visual Studio, is creating issues.

Aleris answered 29/10, 2018 at 15:39 Comment(0)
O
7

According to your description, I could not distinguish your function runtime.

For v1:

You could use both ConfigurationManager.AppSettings and System.Environment.GetEnvironmentVariable to get your connection string in Azure.

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

For v2:

You could use System.Environment.GetEnvironmentVariable and ConfigurationBuilder to get it. Add ExecutionContext parameter, which is used to locate function app directory.

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}");
}

Both of v1 and v2 you set the connection string in application settings. enter image description here

For more details, you could refer to this issue.

Octal answered 26/10, 2018 at 7:50 Comment(0)
D
2

Make sure you prefix your connection string correctly -

  • SQL Server connection strings: Prefixed with SQLCONNSTR_
  • MySQL connection strings: Prefixed with MYSQLCONNSTR_
  • SQL Database (Azure SQL) connection strings: Prefixed with SQLAZURECONNSTR_
  • Custom strings: Prefixed with CUSTOMCONNSTR_

eg: Environment.GetEnvironmentVariable("SQLCONNSTR_MyConnectionString")

Diabetic answered 8/2 at 14:32 Comment(1)
This should be the correct answer.Updraft
A
1

I fixed it by creating a new Azure Function project.

So my conclusion is that making a template from another Azure Function and using that as the new project, in Visual Studio, is creating issues.

Aleris answered 29/10, 2018 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.