senor_chang
senor_chang

Reputation: 51

Azure Function v4 and EF ConnectionString: how to make it work both locally and on Azure

I have an Azure Function (v4) app that uses EF6 to talk to an Azure SQL database.

I understand that you should use the Connection Strings section on Azure to configure your EF connection strings, so in my case something like this: Screenshot of Azure Portal app config

This means that at runtime, the connection string name will be prefixed by SQLAZURECONNSTR_, according to the documentation. So the name Base becomes SQLAZURECONNSTR_Base

But I'm developing locally, which means I have a local.settings.json file, which includes an ConnectionStrings section:

{
  "IsEncrypted": false,
  "Values": {
    ...
  },
  "ConnectionStrings": {
    "Base": "..."
  }
}

Now, what should I do in my code to retrieve this connection string?

If I use the Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Base"), it would work on Azure but not locally.

On the other hand, if I use Environment.GetEnvironmentVariable("ConnectionStrings:Base"), it would work locally but not on Azure.

There are workarounds I can think of, such as determining the environment before making the right call, but they are all kinda bizarre to me, since this should be a pretty common scenario...

Upvotes: 5

Views: 1237

Answers (1)

Mohit Ganorkar
Mohit Ganorkar

Reputation: 2069

  • you can set the name of environment variable which holds the connection string to anything just set the connection string variable name to ConnectionStrings:Base in the portal.

  • Thus you can reuse the same code for local environment and function app which will be deployed.

  • for example: I created a http trigger function which will return ConnectionString:base and in both the function app and local environment it is working.

function.cs

 public static async Task<IActionResult> Run(   [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;
            // here I am reading the connection string 
            
            name = Environment.GetEnvironmentVariable("ConnectionStrings:Base");
            string responseMessage = name ;
            Console.WriteLine(responseMessage);
            return new OkObjectResult(responseMessage);
        }

I have set the connection string to . . .

localsetting.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "ConnectionStrings": {
    "Base": "..."
  }
}

App setting in portal :

enter image description here

output of local environment:

enter image description here

output of function app :

enter image description here

Upvotes: 2

Related Questions