我的Azure Function App定义了ConnectionString。我想从用 dotnet standard 2.0 编写的 C# 函数中检索它。我尝试过添加System.Configuration.ConfigurationManager
到project.json并使用
var str = ConfigurationManager.ConnectionStrings["my string"].ConnectionString;
但我得到了错误
run.csx(24,15): 错误 CS0103: 当前上下文中不存在名称“ConfigurationManager”
如何访问连接字符串?
ConfigurationManager 在 Azure Functions v2 .NET Standard 项目中不可用。 Azure FUnction v2 现在使用ASPNET核心配置.
您可以按照这些说明进行操作。
-
在运行方法中添加第三个参数。
public static async Task<HttpResponseMessage> Run(InputMessage req, TraceWriter log, ExecutionContext context)
-
在run方法中添加以下代码。
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
然后您可以使用此变量来访问应用程序设置。
你可以看到这个博客有关如何在 v2 中使用 AppSettings 和 ConnectionStrings 的说明。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)