I am very confused. I want to get a connection string in an Azure v3 function (.Net Core 3.1).
My local settings looks like
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"DefaultConnection": "bla bla"
}
}
and in the function I do
string defaultConnection = Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection");
This works fine locally but on Azure, defaultConnection is null. I defined the connection under the section Connection strings of the function's Application Settings.
Is my approach correct for Azure function v3?
Get connection informationSign in to the Azure portal. Select SQL Databases from the left-hand menu, and select your database on the SQL databases page. Select Connection strings under Settings and copy the complete ADO.NET connection string. For Azure SQL Managed Instance copy connection string for public endpoint.
An application running in an Azure cloud service can store the connection string in the Azure service configuration schema (. cscfg) file. Add the connection string to the ConfigurationSettings section of the service configuration file.
AzureWebJobsStorage. The Azure Functions runtime uses this storage account connection string for normal operation. Some uses of this storage account include key management, timer trigger management, and Event Hubs checkpoints. The storage account must be a general-purpose one that supports blobs, queues, and tables.
You need the specify the connection string prefix (see documentation):
Environment.GetEnvironmentVariable("CUSTOMCONNSTR_DefaultConnection");
Please note that
Connection strings should only be used with a function app if you are using entity framework. For other scenarios use App Settings.
So if you just want to get the value of DefaultConnection, you can put it under Application settings and you can get it like this
Environment.GetEnvironmentVariable("DefaultConnection");
For Azure function with Entity Framework, please refer to this article.
If you're using Microsoft.NET.Sdk.Functions 3.0.11 and Microsoft.Azure.Functions.Extensions 1.1.0 and you're using Azure Functions with Dependency Injection, you can do the following to access the connection string (or any configuration) when you start the application.
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using System;
[assembly: FunctionsStartup(typeof(YourNamespace.Startup))]
namespace YourNamespace
{
public class Startup : FunctionsStartup
{
public IConfiguration Configuration { get; set; }
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var config = builder.ConfigurationBuilder.Build();
// This can be used to get a connection string from local.settings.json and also works when deployed to Azure
var appConfigConnection = config.GetConnectionString("AppConfig");
// This is to access the environment variables.
var environment = Environment.GetEnvironmentVariable("Environment");
// This particular example uses the values retrieved to bootstrap Azure App Configuration
builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
{
options.Connect(appConfigConnection).Select(KeyFilter.Any, environment.ToLowerInvariant());
});
}
public override void Configure(IFunctionsHostBuilder builder)
{
// In case you need to access IConfiguration
Configuration = builder.GetContext().Configuration;
}
}
}
Sample local.setting.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "your connection string",
"Environment": "Development"
},
"ConnectionStrings": {
"AppConfig": "your connection string"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With