Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 228
Aidan Avatar asked Jan 01 '18 08:01

Aidan


People also ask

How do I connect to Azure function?

Run the Azure Storage Explorer tool, select the connect icon on the left, and select Add an account. In the Connect dialog, choose Add an Azure account, choose your Azure environment, and then select Sign in....

How do I get Azure function connection string?

Get connection informationSign in to the Azure portal. Select SQL Databases from the left-hand menu, and select your database on the SQL databases page. Select Connection strings under Settings and copy the complete ADO.NET connection string.

How do I enable Azure function app?

Sign in to the Azure portal, then search for and select Function App. Select the function you want to verify. In the left navigation under Functions, select App keys. This returns the host keys, which can be used to access any function in the app.


1 Answers

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.

like image 158
Crazy Crab Avatar answered Nov 12 '22 14:11

Crazy Crab