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