How To Access Azure Function App ConnectionString Using dotnet Standard
Asked Answered
L

2

7

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?

Lavery answered 1/1, 2018 at 8:3 Comment(0)
E
15

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.

Episcopate answered 24/2, 2018 at 5:5 Comment(1)
Thank you so much for bringing this to my attention, I had given up, but the new Core Configuration is really neat.Lavery
P
4

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.

enter image description here

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");

enter image description here

enter image description here

Test it on the azure portal.

enter image description here

Pyrone answered 2/1, 2018 at 5:30 Comment(4)
Thanks for the reply. Is your function in a .csx file? It looks like a class to me. Mine is just a static method in a .csx file. I reference the same NuGet package, and use #r "System.Configuration.ConfigurationManager" in the script, but get an error.Lavery
Is your function in a .csx file? No, I used the precompiled azure function.Pyrone
How to reference assemblies on the Azure function you could refer to Azure Functions C# script developer reference.Pyrone
In your case you also could add the connection string in the Azure function appsetting. And get it with following code var connectionstring = Environment.GetEnvironmentVariable("ConnectionString"); , details please refer to the updated answer.Pyrone

© 2022 - 2024 — McMap. All rights reserved.