如何使用 dotnet Standard 访问 Azure Function App ConnectionString

编程入门 行业动态 更新时间:2024-10-10 10:32:28
本文介绍了如何使用 dotnet Standard 访问 Azure Function App ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的 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

    本文发布于:2023-11-14 14:22:12,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1587447.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何使用   Standard   dotnet   Azure   App

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!