如何使用 dotnet Standard 访问 Azure Function App ConnectionString [英] How To Access Azure Function App ConnectionString Using dotnet Standard

查看:20
本文介绍了如何使用 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.

  1. 在 run 方法中添加第三个参数.

  1. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆