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?
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....
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.
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.
ConfigurationManager is not available in Azure Functions v2 .NET Standard projects. Azure FUnction v2 now uses ASPNET Core Configuration.
You can follow these instructions.
Add the 3rd parameter in your run method.
public static async Task<HttpResponseMessage> Run(InputMessage req, TraceWriter log, ExecutionContext context)
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With