Problem accessing settings from an azure function

Anonymous
2021-03-29T23:47:45.227+00:00

I'm writing a rather simple azure function but can't seem to get this basic step to work. The issue I'm having is accessing my connection string from within the code.

I've set the connection string value in a local.settings.json file like below:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "BlueVoltConnection": "Server=tcp:xxx.database.windows.net,1433;Initial Catalog=RC-BV;Persist Security Info=False;User ID=xxxxx;Password=xxxxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
  }
}

In my code I'm trying to load the values thusly:

 private static readonly string _BVProdConnectionString = Environment.GetEnvironmentVariable("BlueVoltConnection");
        

But all I get back is a null. I tried not only having a copy of the local.setting.json in the root folder and in the bin/Debug/netcore1app3.1 folder but I still get null for my value.

Any ideas what I'm doing wrong?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,207 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 68,391 Reputation points
    2021-03-30T08:12:10.477+00:00

    Hi @Anonymous

    Welcome to Microsoft Q&A! Thanks for posting the question.

    You need to define as below for your local.settings.json

      private static readonly string _BVProdConnectionString = Environment.GetEnvironmentVariable("ConnectionStrings:BlueVoltConnection");  
    

    If your local.settings.json would be define as key value pair in Values section as below :

     {  
       "IsEncrypted": false,  
       "Values": {  
         "FUNCTIONS_WORKER_RUNTIME": "dotnet",  
         "AzureWebJobsStorage": "UseDevelopmentStorage=true",  
         "AzureWebJobsDashboard": "UseDevelopmentStorage=true",  
     "BlueVoltConnection": "Server=tcp:xxx.database.windows.net,1433;Initial Catalog=RC-BV;Persist Security Info=False;User ID=xxxxx;Password=xxxxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"  
       },  
       "ConnectionStrings": {  
       }  
     }  
    

    In the above case, you can define it as

      private static readonly string _BVProdConnectionString = Environment.GetEnvironmentVariable("BlueVoltConnection");  
    

    To learn more on local.setting.json you can refer to this.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.