Search code examples
.net-coreconnection-stringazure-functionssqlconnection.net-standard

How To Access Azure Function App ConnectionString Using dotnet Standard


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;

but I get the error

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

How do I access the connection string?


Solution

  • 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.