我的 Azure Function App 定义了 ConnectionString.我想从用 dotnet 标准 2.0 编写的 C# 函数中检索它.我尝试将 System.Configuration.ConfigurationManager 添加到 project.json 并使用
My Azure Function App has a ConnectionString defined. I want to retrieve it from a C# function written in dotnet standard 2.0. I have tried adding System.Configuration.ConfigurationManager to the project.json and using
var str = ConfigurationManager.ConnectionStrings["my string"].ConnectionString;但我收到错误
run.csx(24,15): 错误 CS0103: 当前上下文中不存在名称ConfigurationManager"
run.csx(24,15): error CS0103: The name 'ConfigurationManager' does not exist in the current context
如何访问连接字符串?
推荐答案ConfigurationManager 在 Azure Functions v2 .NET Standard 项目中不可用.Azure FUnction v2 现在使用 ASPNET Core 配置.
ConfigurationManager is not available in Azure Functions v2 .NET Standard projects. Azure FUnction v2 now uses ASPNET Core Configuration.
您可以按照这些说明进行操作.
You can follow these instructions.
在 run 方法中添加第三个参数.
Add the 3rd parameter in your run method. public static async Task<HttpResponseMessage> Run(InputMessage req, TraceWriter log, ExecutionContext context)
在run方法中,添加如下代码.
In the run method, add the following code.
var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build();然后您可以使用此变量访问应用设置.
Then you can use this variable to access app settings.
您可以查看此博客了解如何使用v2 中的 AppSettings 和 ConnectionStrings.
You can see this blog for instructions on how to use AppSettings and ConnectionStrings in v2.
更多推荐
如何使用 dotnet Standard 访问 Azure Function App ConnectionString
发布评论