Azure Functions Database Connection String

前端 未结 14 719
谎友^
谎友^ 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: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 Run(
               [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
               [CosmosDB(databaseName:"dbName",
                         collectionName:"collectionName",
                         ConnectionStringSetting = "CosmosDBConnection")] DocumentClient documentClient, 
               ILogger log){
                 .....
               }
    

提交回复
热议问题