How To Access Azure Function App ConnectionString Using dotnet Standard

我怕爱的太早我们不能终老 提交于 2019-12-01 05:50:45
Crazy Crab

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. Add the 3rd parameter in your run method.

    public static async Task<HttpResponseMessage> Run(InputMessage req, TraceWriter log, ExecutionContext context)
    
  2. 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();
    
  3. Then you can use this variable to access app settings.

You can see this blog for instructions on how to use AppSettings and ConnectionStrings in v2.

run.csx(24,15): error CS0103: The name 'ConfigurationManager' does not exist in the current context

According to mentioned expception. It seems that you need to add reference System.Configuration in the dotnet standard 2.0 class library. I test it locally it works correctly on my side.

public class TestGetConnectionString
{

    public string ConnectionString;

    public TestGetConnectionString()
    { 

        var str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        ConnectionString = str;
    }


}

Update:

In your case you also could add the connection string in the Azure function appsetting. Details you could refer to the screenshot. And we could access it easily by the following code.

 var connectionstring = Environment.GetEnvironmentVariable("ConnectionString");

Test it on the azure portal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!