10

I'm using EF core 2.0 in Azure Functions using .net core. I'm trying to read db ConnectionString from local.settings.json, which is defined:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "MyDbConnStr": "Data Source=.;Initial Catalog=xxxxxxx"
  }
}

Environment.GetEnvironmentVariable() doesn't return any connection string info neither I can use ConfigurationManager.ConnectionStrings with .net core. How do I access the connection string from the code?

6
  • How are you able to load EFCore 2.X with azure functions locally? I tried and it can't load EFCore. Are you using visual studio? Commented Dec 18, 2017 at 20:50
  • @DOMZE yep, using VS 2017, version 15.5.2 with .net core 2 Functions template <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <AzureFunctionsVersion>v2</AzureFunctionsVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.6" /> </ItemGroup> Commented Dec 18, 2017 at 23:13
  • @DOMZE make sure you reference Sdk.Functions 1.0.6 and 2.0.0 EF. There is currently Azure Functions .NET Core bug Commented Dec 18, 2017 at 23:15
  • 1
    @DOMZE github.com/Azure/Azure-Functions/issues/… Commented Dec 18, 2017 at 23:17
  • You my friend are a savior! thanks so much. Commented Dec 18, 2017 at 23:58

2 Answers 2

13

You could use one of the helper classes in Microsoft.Azure.WebJobs.Host:

AmbientConnectionStringProvider.Instance.GetConnectionString("MyDbConnStr")

This class is delegating the work to internal class called ConfigurationUtility, which does something in line with

var configuration = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .AddJsonFile("appsettings.json", true)
    .Build();
var conn = configuration.GetConnectionString("MyDbConnStr");
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! It works! Thanks! It's not documented anywhere. I would never have guessed it...
It is documented here learn.microsoft.com/en-us/aspnet/core/fundamentals/… Configuration has simple enough mechanism to load config from env/json/ini.
How do I use any other configuration variable, not a connection string. It's not working with custom parameters
9

You can use Environment.GetEnvironmentVariable. The keys are "ConnectionStrings:YourKey"

Assuming your local.settings.json looks like the following:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  },
  "ConnectionStrings": {
    "sqldb_connection": "connection_string_here"
  }
}

Use Environment.GetEnvironmentVariable("ConnectionStrings:sqldb_connection", EnvironmentVariableTarget.Process); to get the value

1 Comment

That works during local dev, but how do you set this value in the Azure portal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.