Skip to content

Local Configuration

Mika Berglund edited this page Jul 25, 2019 · 2 revisions

Local Configuration

To be able to run our function application locally, we need to add configuration data to the local.settings.json file, which is added by default when creating a function application in Visual Studio.

Note that even though this is a JSON file and we could add objects with child objects and arrays to the local configuration file, we will not do that, because when we deploy the application to Azure, the application configuration is stored as key-value pairs, so we'll stick with that also locally.

The default local.settings.json file that is created looks like this.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

To specify configuration values as key-value pairs, we use a special colon notation to depict object hierarchies, for instance parentobject:childobject:property

Child Objects

We'll first add the SMTP configuration to our local settings file like this:

{
  "IsEncrypted": false,
  "Values": {
    "myapp:smtp:host": "localhost",
    "myapp:smtp:port": 25,
    "myapp:smtp:username": "SMTPSender",
    "myapp:smtp:password": "pwd"
  }
}

This means that our root configuration class defines a MyApp property which is an instance of the MyAppConfigurationSection class, which defines an Smtp property, which is an instance of the SmtpConfigSection class, which defines the Host, Port, UserName and Password properties.

Configuration Collections

We can use the same notation for collections of configuration information too. We describe each item in a collection with a numeric index in the notation. So we add the following rows to our settings file.

{
  "IsEncrypted": false,
  "Values": {
    "myapp:containers:1:connectionstring": "UseDevelopmentStorage=true",
    "myapp:containers:1:enablesoftdelete": true,
    "myapp:containers:1:container": "container-a",
    "myapp:containers:3:connectionstring": "UseDevelopmentStorage=true",
    "myapp:containers:3:container": "container-b",
    "myapp:containers:3:enablesoftdelete": false
  }
}

Please note that the actual number does not matter. Just use the same number to group values together into one object.

The configuration above means that our MyApp configuration section has a Containers collection that will have two SmtpConfigSection items.

I've deliberately used the indexes 1 and 3 to demonstrate that the actual numbers do not matter. I could just as well have chosen 0 and 1 or 1045 and 4321.

The Full local.settings.json File

The complete local.settings.json then looks like this.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",

    "myapp:smtp:host": "localhost",
    "myapp:smtp:port": 25,
    "myapp:smtp:username": "SMTPSender",
    "myapp:smtp:password": "pwd",

    "myapp:containers:1:connectionstring": "UseDevelopmentStorage=true",
    "myapp:containers:1:enablesoftdelete": true,
    "myapp:containers:1:container": "container-a",
    "myapp:containers:3:connectionstring": "UseDevelopmentStorage=true",
    "myapp:containers:3:container": "container-b",
    "myapp:containers:3:enablesoftdelete": false
  }
}

You can copy this file to your local environment to be able to run the application locally.

Clone this wiki locally