1. <small id='3dXWs'></small><noframes id='3dXWs'>

      <legend id='3dXWs'><style id='3dXWs'><dir id='3dXWs'><q id='3dXWs'></q></dir></style></legend>
        <bdo id='3dXWs'></bdo><ul id='3dXWs'></ul>
    2. <i id='3dXWs'><tr id='3dXWs'><dt id='3dXWs'><q id='3dXWs'><span id='3dXWs'><b id='3dXWs'><form id='3dXWs'><ins id='3dXWs'></ins><ul id='3dXWs'></ul><sub id='3dXWs'></sub></form><legend id='3dXWs'></legend><bdo id='3dXWs'><pre id='3dXWs'><center id='3dXWs'></center></pre></bdo></b><th id='3dXWs'></th></span></q></dt></tr></i><div id='3dXWs'><tfoot id='3dXWs'></tfoot><dl id='3dXWs'><fieldset id='3dXWs'></fieldset></dl></div>
      <tfoot id='3dXWs'></tfoot>

      从 Azure 函数中的 local.settings.json 读取自定义设置

      Read custom settings from local.settings.json in Azure functions(从 Azure 函数中的 local.settings.json 读取自定义设置)
      • <tfoot id='Q9IzW'></tfoot>

      • <i id='Q9IzW'><tr id='Q9IzW'><dt id='Q9IzW'><q id='Q9IzW'><span id='Q9IzW'><b id='Q9IzW'><form id='Q9IzW'><ins id='Q9IzW'></ins><ul id='Q9IzW'></ul><sub id='Q9IzW'></sub></form><legend id='Q9IzW'></legend><bdo id='Q9IzW'><pre id='Q9IzW'><center id='Q9IzW'></center></pre></bdo></b><th id='Q9IzW'></th></span></q></dt></tr></i><div id='Q9IzW'><tfoot id='Q9IzW'></tfoot><dl id='Q9IzW'><fieldset id='Q9IzW'></fieldset></dl></div>
          <legend id='Q9IzW'><style id='Q9IzW'><dir id='Q9IzW'><q id='Q9IzW'></q></dir></style></legend>

              <small id='Q9IzW'></small><noframes id='Q9IzW'>

              • <bdo id='Q9IzW'></bdo><ul id='Q9IzW'></ul>
                  <tbody id='Q9IzW'></tbody>
                本文介绍了从 Azure 函数中的 local.settings.json 读取自定义设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试从 local.settings.json 文件中检索自定义设置.示例我正在尝试读取以下 local.settings.json 文件中存在的表列表

                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?

                推荐答案

                @Kirk 和 @Slava 帮助您摆脱了困惑.只需添加一些细节供您参考.

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

                默认情况下,发布既不会将 local.settings.json 上传到 Azure,也不会基于该本地文件对应用程序设置进行修改,因此我们需要在 Azure 门户上手动更新它们.我们也可以在 VS 发布面板上执行此操作.(如果我们需要在发布前更改设置,请先创建配置文件.)

                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.)

                关于如何在应用设置中获取参数,需要注意的一点是 ConfigurationManager 在 v2 功能(运行时 beta)中不再支持,可能只会得到 null 或异常.对于 v1 函数(runtime ~1),它仍然有效.

                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. 适用于 v1 功能

                1. For v1 function

                要读取 Azure 上的应用程序设置(以及 local.settings.json 中的 Values),建议使用 System.Environment.GetEnvironmentVariable($"{parameterName}").

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

                转到连接字符串,不幸的是 GetEnvironmentVariable 仅适用于 Azure,因为连接字符串(ConnectionStrings in local.settings.json)未导入环境变量.所以我们需要 ConfigurationManager,它可以在 Azure 和本地环境中运行.当然它也可以读取应用程序设置.

                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.

                对于 v2 功能,应用程序设置和连接字符串有两个选择.

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

                一种是使用GetEnvironmentVariable.我们可以参考这个列表 Azure 上连接字符串的前缀.

                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}");
                }
                

                另一种是使用ConfigurationBuilder.添加ExecutionContext参数,用于定位函数应用目录.

                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}");
                }
                

                这篇关于从 Azure 函数中的 local.settings.json 读取自定义设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                <i id='9dhCT'><tr id='9dhCT'><dt id='9dhCT'><q id='9dhCT'><span id='9dhCT'><b id='9dhCT'><form id='9dhCT'><ins id='9dhCT'></ins><ul id='9dhCT'></ul><sub id='9dhCT'></sub></form><legend id='9dhCT'></legend><bdo id='9dhCT'><pre id='9dhCT'><center id='9dhCT'></center></pre></bdo></b><th id='9dhCT'></th></span></q></dt></tr></i><div id='9dhCT'><tfoot id='9dhCT'></tfoot><dl id='9dhCT'><fieldset id='9dhCT'></fieldset></dl></div>

                    <tbody id='9dhCT'></tbody>
                      <bdo id='9dhCT'></bdo><ul id='9dhCT'></ul>

                        <small id='9dhCT'></small><noframes id='9dhCT'>

                          <legend id='9dhCT'><style id='9dhCT'><dir id='9dhCT'><q id='9dhCT'></q></dir></style></legend>
                        • <tfoot id='9dhCT'></tfoot>