6

In Azure function v1, am trying to read the connection string present in local.settings.json using GetEnvironmentVariable static method

Here is my local.settings.json file

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "OnPremisesConnection": "Server=test;Initial Catalog=testdb;Integrated Security=SSPI;"
  }
}

using the following code am reading the connection string

string variableName = "OnPremisesConnection";
var res = Environment.GetEnvironmentVariable($"ConnectionStrings:{variableName}")

but am getting NULL as result. What am I missing here ?

6
  • 1
    Is there a specific reason why you aren't using the ConfigurationManager.ConnectionStrings[variableName] option? I'm not sure if Environment variables should/will work locally. Commented Aug 10, 2018 at 12:54
  • @Jan_V - It is only supported in v1, am advised to use GetEnvironmentVariable for better support in future Commented Aug 10, 2018 at 12:55
  • 1
    @Pரதீப் Big sorry that I made a mistake, if you are still in v1, GetEnvironmentVariable can't get ConnectStrings as they are not imported into environment, only Values are available. I will update my answer in your last question. Commented Aug 10, 2018 at 13:40
  • @JerryLiu Can you please provide your answer to this post as well or link to the 'last question' that you are referring to, in your comment. This would help other readers. Thanks. Commented Aug 10, 2018 at 21:12
  • @KarishmaTiwari-MSFT Sorry about the missing, here it is. Commented Aug 10, 2018 at 23:35

1 Answer 1

0

Ok. You can try the next:

var config = new ConfigurationBuilder()
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
 
 string connectionStr = config["ConnectionStrings:OnPremisesConnection"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.