<bdo id='Wb2rV'></bdo><ul id='Wb2rV'></ul>
  • <legend id='Wb2rV'><style id='Wb2rV'><dir id='Wb2rV'><q id='Wb2rV'></q></dir></style></legend>

    1. <small id='Wb2rV'></small><noframes id='Wb2rV'>

      1. <i id='Wb2rV'><tr id='Wb2rV'><dt id='Wb2rV'><q id='Wb2rV'><span id='Wb2rV'><b id='Wb2rV'><form id='Wb2rV'><ins id='Wb2rV'></ins><ul id='Wb2rV'></ul><sub id='Wb2rV'></sub></form><legend id='Wb2rV'></legend><bdo id='Wb2rV'><pre id='Wb2rV'><center id='Wb2rV'></center></pre></bdo></b><th id='Wb2rV'></th></span></q></dt></tr></i><div id='Wb2rV'><tfoot id='Wb2rV'></tfoot><dl id='Wb2rV'><fieldset id='Wb2rV'></fieldset></dl></div>

        <tfoot id='Wb2rV'></tfoot>

        从 Azure Functions 中的配置文件加载连接字符串

        时间:2023-10-05
          <tfoot id='SyFWQ'></tfoot>
          <i id='SyFWQ'><tr id='SyFWQ'><dt id='SyFWQ'><q id='SyFWQ'><span id='SyFWQ'><b id='SyFWQ'><form id='SyFWQ'><ins id='SyFWQ'></ins><ul id='SyFWQ'></ul><sub id='SyFWQ'></sub></form><legend id='SyFWQ'></legend><bdo id='SyFWQ'><pre id='SyFWQ'><center id='SyFWQ'></center></pre></bdo></b><th id='SyFWQ'></th></span></q></dt></tr></i><div id='SyFWQ'><tfoot id='SyFWQ'></tfoot><dl id='SyFWQ'><fieldset id='SyFWQ'></fieldset></dl></div>

          <legend id='SyFWQ'><style id='SyFWQ'><dir id='SyFWQ'><q id='SyFWQ'></q></dir></style></legend>

        • <small id='SyFWQ'></small><noframes id='SyFWQ'>

          • <bdo id='SyFWQ'></bdo><ul id='SyFWQ'></ul>
              <tbody id='SyFWQ'></tbody>

                1. 本文介绍了从 Azure Functions 中的配置文件加载连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 Azure 函数中,我使用了一个库,它通过 ConfigurationManager 中的 ConnectionString 建立与 SQL 服务器的连接,如下所示:

                  In my Azure Function I am using a Library which establishes a connection to an SQL server via the ConnectionString from the ConfigurationManager like this:

                  var cs = System.Configuration.ConfigurationManager.ConnectionStrings["DbConString"].ConnectionString;
                  DbConnection connection = new SqlConnection(cs);
                  

                  现在,当我通过应用程序设置在门户中设置连接字符串 DbConString 时,一切正常.但是对于本地开发,我使用 azure-functions-cli,不幸的是我不知道应该将连接字符串放在哪里,以便通过 ConfigurationManager 正确加载它.

                  Now when i set the connection string DbConString in the portal via the Application Settings everything is working fine. But for local development I use the azure-functions-cli and unfortunately I have no idea where i should place the connection string to have it loaded correctly via the ConfigurationManager.

                  我尝试将它放在 appsettings.json 文件中,但没有成功.

                  I've tried to place it in the appsettings.json file but without success.

                  我的 appsettings.json 目前看起来像这样:

                  My appsettings.json currently looks like this:

                  {
                    "IsEncrypted": false,
                    "Values": {
                      "AzureWebJobsStorage": "",
                      "AzureWebJobsDashboard": "",
                      "MyServiceBusReader": "Endpoint=sb://xxxx=",
                      "DbConStr1": "data source=(localdb)\MSSQLLocalDB;initial catalog=MyDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework",
                      "ConnectionStrings": {
                        "DbConStr2": "data source=(localdb)\MS..." 
                      } 
                    }
                  }
                  

                  但我无法通过 ConfigurationManager 访问DbConStr1".在ConnectionStrings"中添加DbConStr2",如 here 导致编译错误.可能是因为我没有使用 .NET Core?

                  But I am not able to access "DbConStr1" via the ConfigurationManager. Adding "DbConStr2" within "ConnectionStrings" like described here leads to a compilation error. Maybe because I am not using .NET Core?

                  我搞砸了ConnectionStrings"的嵌套.它必须与值"处于同一嵌套级别:

                  I messed up the nesting of "ConnectionStrings". It has to be on the same nesting level as "Values":

                  {
                    "IsEncrypted": false,
                    "Values": {
                      "AzureWebJobsStorage": "",
                      "AzureWebJobsDashboard": "",
                      "MyServiceBusReader": "Endpoint=sb://xxxx="
                    },
                    "ConnectionStrings": {
                      "DbConStr": "data source=(localdb)\MS..." 
                    }
                  }
                  

                  推荐答案

                  问题是,一个连接字符串从 e.g.Web.config 文件由两部分组成:

                  The problem was, that a connection string known from e.g. a Web.config file consists of two parts:

                  • 连接字符串本身和
                  • 提供者名称.

                  但由于配置文件使用 JSON 格式,因此无法同时指定这两个参数.

                  But since the configuration file uses the JSON format it was not possible to specify both parameters.

                  在提出问题时,无法在 appsetings.json 中设置提供程序名称(现已重命名为 local.settings.json).但是 Azure-Functions 团队对此进行了更改,并将 providerName 的默认值设置为 System.Data.SqlClient,从而解决了问题.

                  At the time when the question was asked, it was not possible to set the provider name in the appsetings.json (now renamed to local.settings.json). But the Azure-Functions-team change this and set a default value for providerName to System.Data.SqlClient, which solved the problem.

                  providerName 默认为 System.Data.SqlClient.您不必手动设置它.只需添加您的连接字符串 X 并通过 ConfigurationManager.ConnectionStrings["X"] 读取它.

                  The providerName defaults to System.Data.SqlClient. You don't have to set it manually. Just add your connection string X and read it via ConfigurationManager.ConnectionStrings["X"].

                  这篇关于从 Azure Functions 中的配置文件加载连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法将参数“日志"绑定到类型 TraceWriter 下一篇:Azure Functions 不发布 appsettings.prod.json 文件

                  相关文章

                        <bdo id='eqrVd'></bdo><ul id='eqrVd'></ul>
                      <tfoot id='eqrVd'></tfoot>
                      <i id='eqrVd'><tr id='eqrVd'><dt id='eqrVd'><q id='eqrVd'><span id='eqrVd'><b id='eqrVd'><form id='eqrVd'><ins id='eqrVd'></ins><ul id='eqrVd'></ul><sub id='eqrVd'></sub></form><legend id='eqrVd'></legend><bdo id='eqrVd'><pre id='eqrVd'><center id='eqrVd'></center></pre></bdo></b><th id='eqrVd'></th></span></q></dt></tr></i><div id='eqrVd'><tfoot id='eqrVd'></tfoot><dl id='eqrVd'><fieldset id='eqrVd'></fieldset></dl></div>
                    1. <small id='eqrVd'></small><noframes id='eqrVd'>

                    2. <legend id='eqrVd'><style id='eqrVd'><dir id='eqrVd'><q id='eqrVd'></q></dir></style></legend>