bdcoder
bdcoder

Reputation: 3781

Unable to read local.settings.json file when running Azure Function locally

I am trying to read an environment variable while testing Azure Functions locally (using v1 .Net 4.7.1)

Below is the function code:

[FunctionName( "test" )]
  public static async Task<HttpResponseMessage> Run( [HttpTrigger( AuthorizationLevel.Anonymous, "post", Route = null )]HttpRequestMessage req, TraceWriter log )
  {

     String s;

     log.Info( "test: Begin..." );

     s = System.Environment.GetEnvironmentVariable( "test" );

     if ( String.IsNullOrEmpty( s ) )
     {
        log.Info( "Unable to get environment key !!!!" );
     }
     else
     {
        log.Info( "Key value = " + s );
     }

  }

The local.settings.json file contains:

{
"IsEncrypted": false,
"Values": {
   "AzureWebJobsStorage": "UseDevelopmentStorage=true",
   "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
   "test": "test_value"
   }
}

The function compiles and runs, but the environment key value is never returned, the log always contains:

 Unable to get environment key !!!!

The weird part is I have another project (same code as above) and it works just fine. The only difference in the assemblies is the Microsoft.Net.Sdk.Functions DLL (1.0.19) vs (1.0.13) -- the current project (that does NOT work) is using 1.0.19 -- so I downgraded to 1.0.13 -- but it made NO DIFFERENCE !

project differences

UPDATE: 9-Sep-2018 - special thanks to Karishma Tiwari who steered me in the right direction:

The problem was (as Karismha pointed out) that the local.settings.json file was NOT being copied to the output path, as shown below by comparing the settings in the project that is NOT working (on left), and the project that is working (right):

enter image description here

To solve:

  1. Right-click on local.settings.json -> Properties
  2. Change the "Copy to Output Directory" setting to: "Copy if newer"

One other point to note is that MS documentation states states:

"... but we recommend that you use GetEnvironmentVariable ...",

Problem solved.

Upvotes: 16

Views: 31136

Answers (3)

foobar
foobar

Reputation: 795

It looks like "local.settings.json" environment file is completelty ignored if any value in "Values" object has an array or object. Azure Functions framework only allows strings to be stored as values.

From documentation:

Collection of application settings used when a project is running locally. These key-value (string-string) pairs correspond to application settings in your function app in Azure, like AzureWebJobsStorage. Many triggers and bindings have a property that refers to a connection string app setting, like Connection for the Blob storage trigger. For these properties, you need an application setting defined in the Values array. See the subsequent table for a list of commonly used settings. Values must be strings and not JSON objects or arrays. Setting names can't include a double underline (__) and shouldn't include a colon (:). Double underline characters are reserved by the runtime, and the colon is reserved to support dependency injection.

So something like this:

{
    "IsEncrypted": false,
    "Values": {
       "AzureWebJobsStorage": ["One", "Two"]
    }
}

Or this:

{
    "IsEncrypted": false,
    "Values": {
       "AzureWebJobsStorage": {
           "Foo": "Bar"
       }
    }
}

Is going to make it look as if "local.settings.json" is not found.

Hope this will help other folks landing on this page.

Upvotes: 5

increddibelly
increddibelly

Reputation: 1239

odds are, you added the file to your project when you created a local copy. scan the git history of your project file for a snippet like this:

<ItemGroup>
  <None Include="local.settings.json" />
</ItemGroup>

Upvotes: 0

Karishma Tiwari - MSFT
Karishma Tiwari - MSFT

Reputation: 1545

I would recommend trying the following:

  1. First of all, try using ConfigurationManager.AppSettings["test"] in place of System.Environment.GetEnvironmentVariable( "test" ) and check if you are getting the expected result.

  2. Make sure your local.settings.json file is marked to always be copied to the build output. (by right clicking on it in Visual studio). This will copy your files to the output path similar to bin\debug..

Let me know if you are still seeing the same issue.

Upvotes: 11

Related Questions