问题描述
在我的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函数中的配置文件加载连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!