Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to retrieve a custom setting from local.settings.json file. Example I am trying to read tables list present in the below local.settings.json file

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "TableList": "TestTableName1,TestTableName2"
  }
}

Using the following code am reading it

string tableslist = ConfigurationManager.AppSettings["TableList"];

and it works, but I've read in a few places that this works only when debugging locally, it might not work after it is deployed, in production environment. Could someone point me how to do this in the right way? Or the problem is only applicable to the connection string related settings?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
267 views
Welcome To Ask or Share your Answers For Others

1 Answer

@Kirk and @Slava have helped you get rid of confusion. Just add some details for you to refer.

By default, publication neither upload local.settings.json to Azure, nor makes modification on Application settings based on that local file, hence we need to update them manually on Azure portal. We can also do that on VS publish panel.(Create profile first if we need to change settings before publish.)

enter image description here

About how to get parameters in app settings, one thing to note is that ConfigurationManager is no long supported in v2 function(runtime beta), may only get null or exception with it. For v1 function(runtime ~1), it still works.

  1. For v1 function

    To read Application settings on Azure(also Values in local.settings.json), System.Environment.GetEnvironmentVariable($"{parameterName}") is recommended.

    Turn to Connection strings, unfortunately GetEnvironmentVariable only works on Azure because Connection strings(ConnectionStrings in local.settings.json) are not imported into Environment Variables. So we need ConfigurationManager, which works in both Azure and local env. Of course it can read Application settings as well.

  2. For v2 function, two choices for both Application settings and Connection strings.

    One is to use GetEnvironmentVariable. We can refer to this list for Prefixes of Connection String on Azure.

    // Get Application settings
    var appParameter= "AzureWebJobsStorage";
    System.Environment.GetEnvironmentVariable($"{appParameter}");
    
    // Get Connection strings(put local and Azure env together)
    var connParameter= "MySqlAzureConnection";
    var Prefix = "SQLAZURECONNSTR_";
    var connectionString = System.Environment.GetEnvironmentVariable($"ConnectionStrings:{connParameter}");
    if(string.IsNullOrEmpty(connectionString )){
       connectionString = System.Environment.GetEnvironmentVariable($"{Prefix}{connParameter}");
    }
    

    Another one is to use ConfigurationBuilder. Add ExecutionContext parameter, which is used to locate function app directory.

    [FunctionName("FunctionName")]
    public static void Run(...,ExecutionContext context)
    {
       //"Values" and "Connection" sections are injected into EnvironmentVariables automatically hence we don't need to load Json file again. 
       //Hence SetBasePath and AddJsonFile are only necessary if you have some custom settings(e.g. nested Json rather than key-value pairs) outside those two sections. It's recommended to put those setting to another file if we need to publish them.
       //Note that Function binding settings(e.g. Storage Connection String) must be EnvironmentVariables, i.e. must be stored in "Values" section.
        var config = new ConfigurationBuilder()
            .SetBasePath(context.FunctionAppDirectory)
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
    
        // Get Application Settings
        var appParameter= "AzureWebJobsStorage";
        string appsetting = config[$"{appParameter}"];
    
        // Get Connection strings
        var connParameter= "MySqlAzureConnection";
        string connectionString = config.GetConnectionString($"{connParameter}");
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...