Not logged in - Login

Config Settings in .Net Core v3

The following illustrates how to set up configuration settings for a Azure Function that is using .NET Core v3.

When running the debugger, the system will pull the configuration values from a local settings file.

Create / edit the local.settings.json file.

{
       "IsEncrypted": false,
       "Values": {
              "AzureWebJobsStorage": "UseDevelopmentStorage=true",
              "FUNCTIONS_WORKER_RUNTIME": "dotnet",
              "myName": "Doug",
              "SQLAZURECONNSTR_TestConnect": "Server=tcp:myazureserver.database.windows.net,1433;Initial Catalog=LoggingTest;Persist Security Info=False;User ID=myuser;Password={mypassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
       }
}

Notice that the connection string setting name is prefixed with 'SQLAZURECONNSTR_'. When setting the connection string the the Azure function configuration, exclude this prefix. In this example, it would be named 'TestConnect'.

Add code in your function to retrieve the config settings. You may need to a the Microsoft.Extensions.Configuration NuGet package to your project.

using Microsoft.Extensions.Configuration;

namespace YourFunctionNamespace
{
      [FunctionName("Function1")]
      public static async Task<IActionResult> Run(
         [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
         ExecutionContext context,
         ILogger log)
      {
         // initialize our config settings
         var config = InitializeSettings(context);

         // get our setting from the config
         string testSetting = config["myName"];

         // get our connection string from the config
         string connectString = config.GetConnectionString("TestConnect");

         return new OkObjectResult("Hello World!");
      }

      private static IConfigurationRoot InitializeSettings(ExecutionContext context)
      {
         var config = new ConfigurationBuilder()
            .SetBasePath(context.FunctionAppDirectory)
            // This gives you access to your application settings in your local development environment
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            // This is what actually gets you the application settings in Azure
            .AddEnvironmentVariables()

            .Build();

         return config;
      }
}