Search code examples
c#asp.net-coreazure-functionsconnection-stringapplication-settings

Get Connection String in Azure Function v3


I am very confused. I want to get a connection string in an Azure v3 function (.Net Core 3.1).

My local settings looks like

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
    },
    "ConnectionStrings": {
      "DefaultConnection": "bla bla"
    }
}

and in the function I do

string defaultConnection = Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection");

This works fine locally but on Azure, defaultConnection is null. I defined the connection under the section Connection strings of the function's Application Settings.

enter image description here

Is my approach correct for Azure function v3?


Solution

  • You need to specify the connection string prefix (see documentation):

    Environment.GetEnvironmentVariable("CUSTOMCONNSTR_DefaultConnection");