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

查看:101
本文介绍了如何使用dotnet标准访问Azure Function App ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Azure Function应用定义了一个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

如何访问连接字符串?

推荐答案

Azure Functions v2 .NET Standard项目中没有ConfigurationManager。 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. 在您的计算机上添加第三个参数

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

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