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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for bringing this to my attention, I had given up, but the new Core Configuration is really neat.
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

4 Comments

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.
Is your function in a .csx file? No, I used the precompiled azure function.
How to reference assemblies on the Azure function you could refer to Azure Functions C# script developer reference.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.