3
votes

I created an HTTP Trigger function in Azure with the hope of reading data from an Azure SQL database. I configured the connection string in the Connection Strings section of the Function App as shown in the image below

Connection String Error

I used the below code to try and read the connection string.

var cnnString = ConfigurationManager.ConnectionStrings["TempleDataContext"].ConnectionString;

On execution i see the below error in the logs.

2018-10-24T18:13:56.385 [Information] Script for function 'donors1' changed. Reloading. 2018-10-24T18:13:56.601 [Information] Compilation succeeded. 2018-10-24T18:13:58.511 [Information] Executing 'Functions.donors1' (Reason='This function was programmatically called via the host APIs.', Id=8b140087-4b50-450e-829c-f7abd9e3a1eb) 2018-10-24T18:13:59.067 [Information] C# HTTP trigger function processed a request. 2018-10-24T18:13:59.245 [Error] Executed 'Functions.donors1' (Failed, Id=8b140087-4b50-450e-829c-f7abd9e3a1eb) Object reference not set to an instance of an object.

I wanted to test what all were coming back on the ConnectionStrings object and changed the code to

var cnnStrings = ConfigurationManager.ConnectionStrings;
    foreach(var c in cnnStrings)
    {
        log.LogInformation(c.ToString());
    }

When I check the logs, this is the info I see.

2018-10-24T18:16:53.755 [Information] Script for function 'donors1' changed. Reloading. 2018-10-24T18:16:54.540 [Information] Compilation succeeded. 2018-10-24T18:16:55.917 [Information] Executing 'Functions.donors1' (Reason='This function was programmatically called via the host APIs.', Id=508c49bf-d5e6-4a99-b1db-e1a79545fa91) 2018-10-24T18:16:56.063 [Information] C# HTTP trigger function processed a request. 2018-10-24T18:16:56.070 [Information] data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true 2018-10-24T18:16:56.079 [Information] Executed 'Functions.donors1' (Succeeded, Id=508c49bf-d5e6-4a99-b1db-e1a79545fa91)

I am confused as to where it is getting the ConnectionString information.

Just to test a thoery I also added a few lines to read app settings

var appSettings = ConfigurationManager.AppSettings;
foreach(var a in appSettings)
{
    log.LogInformation(a.ToString());
}

It did not show me any information for these lines.

I am writing the Azure Functions directly in the portal using C# Script. Am I missing something? Any help would be greatly appreciated.

2

2 Answers

6
votes

In function v2, you should use Environment.GetEnvironmentVariable("string_name",EnvironmentVariableTarget.Process) to get values from Application settings and connection strings.

Note for the connection strings:

when use the above method, the first parameter depends on the Type. It means that when the type of the connection string is SQLAZURE, then the first parameter should be SQLAZURE + "CONNSTR" + "_stringName". The screenshot is as below: enter image description here

The code sample as below:

    //for connection string
    string connStr = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_sqldb_connection",EnvironmentVariableTarget.Process);
    log.LogInformation("the connection string is: " + connStr);

    //for app settings
    string appStr = Environment.GetEnvironmentVariable("AzureWebJobsStorage",EnvironmentVariableTarget.Process);
    log.LogInformation("the string in app settings is: " + appStr);

The test result as below: enter image description here

2
votes

I ran into the same thing. Unless you are using Entity Framework (as the image says), the connection string does not get attached. I just put my connection string in the appsettings and moved on.