Azure Functions Database Connection String

前端 未结 14 725
谎友^
谎友^ 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:43

    Addition to the answer from @ToddDeLand.

    With a local.settings.json like this:

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

    You can then access your connection string like this, no NuGets needed.

    var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings:MyConnectionString");
    

    Microsoft recommends this approach here:

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables

    If you add the connection string to values:

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

    You can access your connection string like this:

    var connectionString = Environment.GetEnvironmentVariable("MyConnectionString");
    

    Sources:

    https://stackoverflow.com/a/52219491/3850405

    https://github.com/Azure/Azure-Functions/issues/717#issuecomment-400098791

提交回复
热议问题