Azure Function无法读取应用程序设置和连接字符串 [英] Azure Function unable to read app settings and connection strings
问题描述
我在Azure中创建了HTTP触发器函数,希望从Azure SQL数据库中读取数据.我在Function App的Connection Strings部分中配置了连接字符串,如下图所示
I created an HTTP Trigger function in Azure with the hope of reading data from an Azure SQL database. I configured the connection string in the Connection Strings section of the Function App as shown in the image below
我使用下面的代码尝试读取连接字符串.
I used the below code to try and read the connection string.
var cnnString = ConfigurationManager.ConnectionStrings["TempleDataContext"].ConnectionString;
执行时,我在日志中看到以下错误.
On execution i see the below error in the logs.
2018-10-24T18:13:56.385 [Information]函数'donors1'的脚本 改变了.正在重新加载. 2018-10-24T18:13:56.601 [信息]编译 成功了. 2018-10-24T18:13:58.511 [信息]正在执行 'Functions.donors1'(Reason ='此函数以编程方式被调用 通过主机API.',Id = 8b140087-4b50-450e-829c-f7abd9e3a1eb) 2018-10-24T18:13:59.067 [信息] C#HTTP触发函数 处理了一个请求. 2018-10-24T18:13:59.245 [错误]已执行 'Functions.donors1'(失败,ID = 8b140087-4b50-450e-829c-f7abd9e3a1eb) 对象引用未设置为对象的实例.
2018-10-24T18:13:56.385 [Information] Script for function 'donors1' changed. Reloading. 2018-10-24T18:13:56.601 [Information] Compilation succeeded. 2018-10-24T18:13:58.511 [Information] Executing 'Functions.donors1' (Reason='This function was programmatically called via the host APIs.', Id=8b140087-4b50-450e-829c-f7abd9e3a1eb) 2018-10-24T18:13:59.067 [Information] C# HTTP trigger function processed a request. 2018-10-24T18:13:59.245 [Error] Executed 'Functions.donors1' (Failed, Id=8b140087-4b50-450e-829c-f7abd9e3a1eb) Object reference not set to an instance of an object.
我想测试在ConnectionStrings对象上返回的所有内容,并将代码更改为
I wanted to test what all were coming back on the ConnectionStrings object and changed the code to
var cnnStrings = ConfigurationManager.ConnectionStrings;
foreach(var c in cnnStrings)
{
log.LogInformation(c.ToString());
}
当我检查日志时,这就是我看到的信息.
When I check the logs, this is the info I see.
2018-10-24T18:16:53.755 [Information]函数'donors1'的脚本 改变了.正在重新加载. 2018-10-24T18:16:54.540 [信息]编译 成功了. 2018-10-24T18:16:55.917 [信息]正在执行 'Functions.donors1'(Reason ='此函数以编程方式被调用 通过主机API.',Id = 508c49bf-d5e6-4a99-b1db-e1a79545fa91) 2018-10-24T18:16:56.063 [信息] C#HTTP触发函数 处理了一个请求. 2018-10-24T18:16:56.070 [信息]数据 source =.\ SQLEXPRESS;集成 安全性= SSPI; AttachDBFilename = | DataDirectory | aspnetdb.mdf;用户 Instance = true 2018-10-24T18:16:56.079 [信息]已执行 'Functions.donors1'(成功, id = 508c49bf-d5e6-4a99-b1db-e1a79545fa91)
2018-10-24T18:16:53.755 [Information] Script for function 'donors1' changed. Reloading. 2018-10-24T18:16:54.540 [Information] Compilation succeeded. 2018-10-24T18:16:55.917 [Information] Executing 'Functions.donors1' (Reason='This function was programmatically called via the host APIs.', Id=508c49bf-d5e6-4a99-b1db-e1a79545fa91) 2018-10-24T18:16:56.063 [Information] C# HTTP trigger function processed a request. 2018-10-24T18:16:56.070 [Information] data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true 2018-10-24T18:16:56.079 [Information] Executed 'Functions.donors1' (Succeeded, Id=508c49bf-d5e6-4a99-b1db-e1a79545fa91)
对于从何处获取ConnectionString信息,我感到困惑.
I am confused as to where it is getting the ConnectionString information.
为了测试一种方法,我还添加了几行内容来读取应用程序设置
Just to test a thoery I also added a few lines to read app settings
var appSettings = ConfigurationManager.AppSettings;
foreach(var a in appSettings)
{
log.LogInformation(a.ToString());
}
它没有显示这些行的任何信息.
It did not show me any information for these lines.
我正在使用C#脚本直接在门户中编写Azure函数.我想念什么吗?任何帮助将不胜感激.
I am writing the Azure Functions directly in the portal using C# Script. Am I missing something? Any help would be greatly appreciated.
推荐答案
在功能v2中,您应使用Environment.GetEnvironmentVariable("string_name",EnvironmentVariableTarget.Process)
从应用程序设置和连接字符串中获取值.
In function v2, you should use Environment.GetEnvironmentVariable("string_name",EnvironmentVariableTarget.Process)
to get values from Application settings and connection strings.
有关连接字符串的说明:
Note for the connection strings:
使用上述方法时,第一个参数取决于类型.这意味着当连接字符串的类型为SQLAZURE
时,第一个参数应为SQLAZURE +"CONNSTR" +"_stringName".屏幕截图如下:
when use the above method, the first parameter depends on the Type. It means that when the type of the connection string is SQLAZURE
, then the first parameter should be SQLAZURE + "CONNSTR" + "_stringName". The screenshot is as below:
代码示例如下:
//for connection string
string connStr = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_sqldb_connection",EnvironmentVariableTarget.Process);
log.LogInformation("the connection string is: " + connStr);
//for app settings
string appStr = Environment.GetEnvironmentVariable("AzureWebJobsStorage",EnvironmentVariableTarget.Process);
log.LogInformation("the string in app settings is: " + appStr);
测试结果如下:
这篇关于Azure Function无法读取应用程序设置和连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!