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

编程入门 行业动态 更新时间:2024-10-23 18:26:51
本文介绍了从Azure函数中的配置文件加载连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的Azure函数中,我正在使用一个库,该库通过ConfigurationManager中的ConnectionString建立到SQL Server的连接,如下所示:

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". 如所述此处会导致编译错误.也许是因为我没有使用.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?

Edit2: 我搞砸了"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..." } }

推荐答案

问题是,例如从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-team团队对此进行了更改,并将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函数中的配置文件加载连接字符串

本文发布于:2023-11-15 23:54:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   配置文件   函数   加载   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!