Azure Functions Database Connection String

前端 未结 14 681
谎友^
谎友^ 2020-12-05 17:08

How do I add or access an app.config file in Azure functions to add a database connection string?

If you\'re not supposed to add an app.config

相关标签:
14条回答
  • 2020-12-05 17:18

    Jan_V almost nailed it, which led me to experiment with this in the local.settings.json

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true;",
        "AzureWebJobsDashboard": ""
      },
      "ConnectionStrings": {
        "MyConnectionString": "[YourConnectionStringHere]"
      }
    }
    

    This allows you to use the ConfigurationManager.ConnectionStrings[] we are all used to.

    var sqlConnection = ConfigurationManager
                       .ConnectionStrings["MyConnectionString"].ConnectionString;
    
    0 讨论(0)
  • 2020-12-05 17:21

    Below worked for me both locally & in Azure for an http trigger function that queries cosmos db

    added Microsoft.Azure.WebJobs.Extensions.CosmosDB nuget package reference to project

    connection string settings:

    local.settings.json

    {
      "ConnectionStrings": {
        "CosmosDBConnection": "AccountEndpoint=foobar;"
      }
    }
    

    in Azure portal > function apps > platform features > configurations > Application settings > New application settings > Name: CosmosDBConnection Value: AccountEndpoint=foobar; update > save

    sample c# Azure function

    public static async Task<IActionResult> Run(
               [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
               [CosmosDB(databaseName:"dbName",
                         collectionName:"collectionName",
                         ConnectionStringSetting = "CosmosDBConnection")] DocumentClient documentClient, 
               ILogger log){
                 .....
               }
    
    0 讨论(0)
  • 2020-12-05 17:27

    I believe common practice is use environment variables for azure functions, then you can setup the environment variables in the Azure Function:

    (Function App Settings -> Configure app settings -> App settings section)

    Maybe would be more helpful if you can also let us know which language you are using?

    0 讨论(0)
  • 2020-12-05 17:28

    Todd De Land's answer only works for local environment. However per this doc, published Azure Function needs connection strings be stored as app settings and retrieved by GetEnvironmentVariable.

    Adding System.Configuration assembly reference is unnecessary.

    string cs = Environment.GetEnvironmentVariable("MyConnectionString",EnvironmentVariableTarget.Process);
    

    Here are the steps to make environment strings retrievable for both local and published environment

    1. To support local environment, in local.settings.json, specify your connection strings inside Values node

    1. To support published environment, go to portal.azure.com > your Azure Function > function node > Application Settings

    1. Finally, call GetEnvironmentVariable from your Azure Function (cant get stackoverflow to display this code correctly)

    Thats it.

    0 讨论(0)
  • 2020-12-05 17:28

    You should store connection string in azure key vault and enable MSI on azure function and add access policy on azure key vault to read key value.

    0 讨论(0)
  • 2020-12-05 17:35

    The best way to do this is to add a Connection String from the Azure portal:

    • From your Function App UI, click Function App Settings
    • Settings / Application Settings
    • Add connection strings

    They will then be available using the same logic as if they were in a web.config, e.g.

    var conn = System.Configuration.ConfigurationManager
                     .ConnectionStrings["MyConn"].ConnectionString;
    

    Or if you're using a non-.NET language, you can use App Settings instead, which become simple environment variables at runtime that your functions can access.

    0 讨论(0)
提交回复
热议问题