Azure Functions Database Connection String - c#

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 and there is a better way to access an external database to execute the function please let me know best practices. Thanks!

Jan_V almost nailed it, which led me to experiment with this in the local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true;",
"AzureWebJobsDashboard": ""
},
"ConnectionStrings": {
"MyConnectionString": "[YourConnectionStringHere]"
}
}
This allows you to use the ConfigurationManager.ConnectionStrings[] we are all used to.
var sqlConnection = ConfigurationManager
.ConnectionStrings["MyConnectionString"].ConnectionString;

The best way to do this is to add a Connection String from the Azure portal:
From your Function App UI, click Function App Settings
Settings / Application Settings
Add connection strings
They will then be available using the same logic as if they were in a web.config, e.g.
var conn = System.Configuration.ConfigurationManager
.ConnectionStrings["MyConn"].ConnectionString;
Or if you're using a non-.NET language, you can use App Settings instead, which become simple environment variables at runtime that your functions can access.

Configuration Manager will be replaced by the new Asp.Net Core Configuration System in Functions Runtime v2.
So if you are using .Net Core you should follow John Gallants Blog article:
https://blog.jongallant.com/2018/01/azure-function-config/
Works with local.settings.json and Settings in Azure Function
Works with App Settings and Connection Strings

Todd De Land's answer only works for local environment. However per this doc, published Azure Function needs connection strings be stored as app settings and retrieved by GetEnvironmentVariable.
Adding System.Configuration assembly reference is unnecessary.
string cs = Environment.GetEnvironmentVariable("MyConnectionString",EnvironmentVariableTarget.Process);
Here are the steps to make environment strings retrievable for both local and published environment
To support local environment, in local.settings.json, specify your connection strings inside Values node
To support published environment, go to portal.azure.com > your Azure Function > function node > Application Settings
Finally, call GetEnvironmentVariable from your Azure Function (cant get stackoverflow to display this code correctly)
Thats it.

I went through several similar questions and answers here. Many of them are either misleading or assuming everybody is on the same level and understands how the azure functions are working. there is no answer for newbies like me. I would like to summarize here my solution step by step.
most important thing is that we understand local.settings.json file
IS NOT FOR AZURE. it is to run your app in the local as the name is
clearly saying. So solution is nothing to do with this file.
App.Config or Web.Config doesnt work for Azure function connection strings. If you have Database Layer Library you cant overwrite connection string using any of these as you would do in Asp.Net applications.
In order to work with, you need to define your connection string on the azure portal under the Application Settings in your Azure function. There is
Connection strings. there you should copy your connection string of your DBContext. if it is edmx, it will look like as below. There is Connection type, I use it SQlAzure but I tested with Custom(somebody claimed only works with custom) works with both.
metadata=res:///Models.myDB.csdl|res:///Models.myDB.ssdl|res://*/Models.myDB.msl;provider=System.Data.SqlClient;provider
connection string='data source=[yourdbURL];initial
catalog=myDB;persist security info=True;user
id=xxxx;password=xxx;MultipleActiveResultSets=True;App=EntityFramework
After you set this up, You need to read the url in your application and provide the DBContext. DbContext implements a constructor with connection string parameter. By default constructor is without any parameter but you can extend this. if you are using POCO classes, you can amend the DbContext class simply. If you use Database generated Edmx classes like I do, you don't want to touch the auto generated edmx classes, instead you want to create partial class in the same namespace and extend this class as below.
This is auto generated DbContext
namespace myApp.Data.Models
{
public partial class myDBEntities : DbContext
{
public myDBEntities()
: base("name=myDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}
this is the new partial class, you create
namespace myApp.Data.Models
{
[DbConfigurationType(typeof(myDBContextConfig))]
partial class myDBEntities
{
public myDBEntities(string connectionString) : base(connectionString)
{
}
}
public class myDBContextConfig : DbConfiguration
{
public myDBContextConfig()
{
SetProviderServices("System.Data.EntityClient",
SqlProviderServices.Instance);
SetDefaultConnectionFactory(new SqlConnectionFactory());
}
}
}
After all you can get the connection string from azure settings, in your Azure Function project with the code below and provide to your DbContext
myDBEntities is the name you gave in the azure portal for your connection string.
var connString = ConfigurationManager.ConnectionStrings["myDBEntities"].ConnectionString;
using (var dbContext = new myDBEntities(connString))
{
//TODO:
}

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://learn.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

I believe common practice is use environment variables for azure functions, then you can setup the environment variables in the Azure Function:
(Function App Settings -> Configure app settings -> App settings section)
Maybe would be more helpful if you can also let us know which language you are using?

If you are using function runtime v3, then following approach will work for you.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true;",
"AzureWebJobsDashboard": ""
},
"ConnectionStrings": {
"MyConnectionString": "[YourConnectionStringHere]"
}
}
In your function's startup file
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var config = builder.ConfigurationBuilder.Build();
var connectionString = config.GetConnectionString("MyConnectionString");
}

I have tried below code snippet on my local database that seems easy. Let's have a look.
Nuget Extention:
Download following reference from Nuget Package Manager On your project Dependencies part
using System.Data.SqlClient;
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"sqldb_connection": "Data Source=.;Initial Catalog=DatabaseName;Connection Timeout=30;Integrated Security=True;"
}
}
Read Connection On Function Body:
//Read database Connection
var sqlConnection = Environment.GetEnvironmentVariable("sqldb_connection");
Function Read Write Operation Using Connection String:
// Convert all request perameter into Json object
var content = req.Content;
string jsonContent = content.ReadAsStringAsync().Result;
dynamic requestPram = JsonConvert.DeserializeObject<AzureSqlTableClass>(jsonContent);
// Validate required param
if (string.IsNullOrEmpty(requestPram.FirstName))
{
return req.CreateResponse(HttpStatusCode.OK, "Please enter First Name!");
}
if (string.IsNullOrEmpty(requestPram.LastName))
{
return req.CreateResponse(HttpStatusCode.OK, "Please enter Last Name!");
}
//Read database Connection
var sqlConnection = Environment.GetEnvironmentVariable("sqldb_connection");
var responseResults = 0;
//Read Write Uisng Connection String
using (SqlConnection conn = new SqlConnection(sqlConnection))
{
conn.Open();
var text = "INSERT INTO AzureSqlTable VALUES ('" + requestPram.FirstName + "', '" + requestPram.LastName + "', '" + requestPram.Email + "') ";
using (SqlCommand cmd = new SqlCommand(text, conn))
{
responseResults = await cmd.ExecuteNonQueryAsync();
}
conn.Close();
}
return req.CreateResponse(HttpStatusCode.OK, responseResults);
Note: While publish your function on azure portal just replace the connection string on local.settings.json file. It will work
accordingly. See the screen shot below:

Try this method.
public static string GetConnectionString(string name)
{
string conStr = System.Environment.GetEnvironmentVariable($"ConnectionStrings:{name}",
EnvironmentVariableTarget.Process);
// Azure Functions App Service naming
if (string.IsNullOrEmpty(conStr))convention
conStr = System.Environment.GetEnvironmentVariable($"SQLAZURECONNSTR_{name}",
EnvironmentVariableTarget.Process);
return conStr;
}

some of the above suggestions work. However there is a more straight forward way of setting a connection string. It is by using the 'publish' screen one sees after hitting the publish setting. see picture from documentation here

The Best way to handle connection strings is by using "Azure Key Vault". You can store all the important secrets in Key Vault and consume in the Application.
As suggested by other members you can use Application Settings.
help full links: https://learn.microsoft.com/en-us/azure-stack/user/azure-stack-key-vault-manage-portal?view=azs-2002
https://learn.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-3.1

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<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
[CosmosDB(databaseName:"dbName",
collectionName:"collectionName",
ConnectionStringSetting = "CosmosDBConnection")] DocumentClient documentClient,
ILogger log){
.....
}

You should store connection string in azure key vault and enable MSI on azure function and add access policy on azure key vault to read key value.

Azure Functions Version 4: How to configure your connection string.
Concepts:
A. There are four versions of Azure Functions. The below was written for version 4. Edit your project file if you are unsure of which version you are using. <AzureFunctionsVersion>v4</AzureFunctionsVersion>
B. You must configure both the local dev environment, and the server environment separately. They are different and require different configuration, but the code which uses the configuration will be the same.
C. Use environmental variables. The usual appconfig.json file is not supported in Azure Functions, being a lightweight execution environment. (You could also use AzureKey vault but that is a different topic.)
To set your dev environment variable, add it to local.settings.json Note that this file is NOT published, and applies only to your local machine as the name implies.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"DefaultConnectionString": "MyConnectionString"
}
}
For your published environment, go into the Azure Portal and set it there.

Related

A named connection string was used, but the name 'DefaultConnection' was not found in the application's configuration

I have a DbContext named FileManagerContext in my DOTNET 6 API:
public class FileManagerContext : DbContext {
public FileManagerContext(DbContextOptions<FileManagerContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
}
}
It's a pretty simple DbContext with a simple Entity in it. Anyway, I have this appsettings.json too:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=FM;User=SA;Password=1234;"
},
"AllowedHosts": "*"
}
And here is the startup snippet in Program.cs's top level statement:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<FileManagerContext>(
opt => opt.UseSqlServer("name=DefaultConnection"));
I could use migrations in the case. All thing goes good. I can add migrations and I can update database successfully. But when I run the application and try to use DbContext I get this error:
System.InvalidOperationException: A named connection string was used,
but the name 'DefaultConnection' was not found in the application's
configuration. Note that named connection strings are only supported
when using 'IConfiguration' and a service provider, such as in a
typical ASP.NET Core application. See
https://go.microsoft.com/fwlink/?linkid=850912 for more information.
I've also tried to get the connection string like this:
var cs = builder.Configuration.GetConnectionString("DefaultConnection");
But it returns null. Can anybody help me through please?
From the sounds of the error, your configuration might not be getting picked up.
How is the configuration being created?
Do you see AddConfiguration extension method being called on the Webhost? the contents of the method should look something like the following:
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, false)
.AddJsonFile($"appsettings.{envLower}.json", true, true)
.AddEnvironmentVariables()
.Build();
The the call to build may or may not exist. We typically manually create the configuration object because we need to construct the Loggers and then use AddConfiguration extension method to add that object to the host.
If you don't see that, please take a look at the documentation from Microsoft for guidance on how to set it up. Configuration Documentation
Alternatively, you can get the connection string via the Configuration Object and pass it to the UseSqlServer method.
services.AddDbContext<FileManagerContext>((provider, options) => {
IConfiguration config = provider.GetRequiredService<IConfiguration>();
string connectionString = config.GetConnectionString("DefaultConnection");
options.UseSqlServer(connectionString);
});
in the alternate method, a service provider is passed to the action. You can use the provider to fetch the IConfiguration object from the DI container.
use builder.Configuration
builder.Services.AddDbContext<FileManagerContext>(options =>
{
options.UseSqlServer(builder.Configuration["ConnectionStrings:DefaultConnection"]);
});
or builder.Configuration.GetConnectionString as #Serge mentions
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
Don't use "name=DefaultConnection" since it is treated as connection string name including "name="
I am using this syntax in program.cs and everything is working properly
var builder = WebApplication.CreateBuilder(args);
.......
builder.Services.AddDbContext<FileManagerContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
and since you are using Ms Sql Server , the connection string for sql server is usually like this
"DefaultConnection": "Data Source=xxxx;Initial catalog=xxx;Integrated Security=False;User ID=xxxx;Password=xxxx;"
and my OnConfiguring code looks like this
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
...your code
}
}
Thanks To #Neil I figured it out. The configurations were not loaded into app. But, since I'm in dotnet 6's top level statements, adding configurations seems a little bit different from #Neil's advice. So here is the working solution:
builder.Services.AddControllers();
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var environmentName = builder.Environment.EnvironmentName;
builder.Configuration
.SetBasePath(currentDirectory)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
.AddEnvironmentVariables();
// blah blah
builder.Services.AddDbContext<FileManagerContext>(
opt => opt.UseSqlServer("name=DefaultConnection"));
I was trying to scaffold the DB changes by this command :
Scaffold-DbContext -Connection name=YourDBContextObjectName -Provider
Microsoft.EntityFrameworkCore.SqlServer -project Destination_NameSpace
-context YourDBContextObjectName -OutputDir Entities -force
But the issue is the same in the code-first approach as well, In my scenario, I had different appsettings for different ENVs.
The appSettings.json was like so (as you can see, there is connectionStrings information in this file:
Also, my appsettings.dev.json was like so :
The problem is being occurred because when I tried to scaffold the DB changes by command, Firstly it tries to build your project, and because the Redis and RabbitMQ connection information is missed in the appsettings.json file, It cannot connect to the dataSources. so It returns the error.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Object reference not set to an instance of an object.
System.InvalidOperationException: A named connection string was used, but the name 'DBContextName' was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider...
Solution :
Set The correct Project as the Startup project
Add missing information about other connections to the appsettings.json
As you see, regardless of the error, It's not always related to the Database connection string.
I had the same problem but the error occurs when the application was published on the server. My string connection is in the user's secret, what I did was the following:
In program.cs
// Add services to the container.
builder.Services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer("Name=dbconnection");
});
builder.Services.AddDbContext<DataContext>();
And before publish, select the string connection in data base options and make sure it is the correct:
Publish Options
It works for me in dot 6 API
Hello, the highlighted code worked for

Overriding how serenity framework handles [ConnectionKey] attribute

Serenity has the class attribute ConnectionKey which allows you to specify the key for which connection string you want to use.
Code Example below:
[ConnectionKey("Default"), Module("Administration"), TableName("Languages")]
[DisplayName("Languages"), InstanceName("Language")]
[ReadPermission(PermissionKeys.Translation)]
[ModifyPermission(PermissionKeys.Translation)]
[LookupScript(typeof(Lookups.LanguageLookup))]
public sealed class LanguageRow : Row, IIdRow, INameRow
{
[DisplayName("Id"), Identity]
public Int32? Id
{
get { return Fields.Id[this]; }
set { Fields.Id[this] = value; }
}
}
In my case I am making use of AWS Secret Manager to hide any sensitive information so the connection string itself isn't sitting in my appsettings but rather an AWS secret key. Therefore when this code gets hit it throws an error because the secret key isn't a valid connection string.
To get the actual connection string I first need to make a request to the AWS Secret Manager.
In their documentation about a third of the way down the page, in the SqlConnections.New method section, they mention how I can specify a connection string that doesn't exist in my appsettings. However, I don't see how I could apply this solution to my particular problem.
Is there a way to override how Serenity handles this ConnectionKey or any other workaround for this problem?
Serenity has connection source what injected by dependency injection. You can find injection line there. If you want to manage it yourself just remove AddSqlConnections from startup and add manually your connection string source what implement IConnectionStrings interface.
There is default IConnectionStrings implementation what you can use as example.
My final solution to the problem was to make use of the static method inside the SqlConnections class, SetConnection which allowed me to associate a different connection string with the default connectionKey.
var csInfo = SqlConnections.GetConnectionString("SecretConnectionKey");
var connectionString = SecretManagerService.GetSecretAsync(csInfo.ConnectionString, RegionEndpoint.USEast1).Result;
SqlConnections.SetConnection("Default", connectionString, csInfo.ProviderName);
This piece of code was placed at the bottom of the Configure method in startup.
For this to work you would also need to add an empty default ConnectionString inside your appsettings.json files.
{
"Data": {
"SecretConnectionKey": {
"ConnectionString": "secretkey",
"ProviderName": "System.Data.SqlClient"
},
"Default": {
"ConnectionString": "",
"ProviderName": "System.Data.SqlClient"
}
},
I recognise that this is indeed a hacky approach but a time effective solution couldn't be found and this would be replaced with the upgrade to .Net 5.0.

Is it possible to Pass Azure Function ServiceBusTrigger connection at runtime

My Code Look like this :
public class Startup : IWebJobsStartup
{
public async void Configure(IWebJobsBuilder builder)
{
Get Connection string Via HTTP Service.
ServiceBusConnectionString = jArray["connectionString"].ToString();
}
}
And I want to Pass Connection Like this
[FunctionName("FunctionTopicMessageLogger")]
public void Run([ServiceBusTrigger("topic", "FunctionTopicMessageLogger",Connection = **ServiceBusConnectionString** )]Message mySbMsg)
{
}
This is my Local.setting.json file for first time
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"Connectinstring": " "
}
}
Problem is that connection only take Const Parameter (So it's not allow to change) So i am trying to write in a local.setting.json and trying to read it from there, I am getting error first time when i am running , and from second time connection is there so it's working fine.
Is i am doing something wrong Please suggest.
There are two important parts in the solution for your problem.
During creation of Azure Function with Service Bus trigger you have to specify the name of the connection string parameter that will be used to retrieve connection string value. In this case if you type "ServiceBusConnectionString" you have to add such configuration parameter in the local.settings.json file in the "ConnectionStrings" section:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"ConnectionStrings": {
"ServiceBusConnectionString": "xxx"
}
}
After depoying your function to Azure, you should use "Configuration" section in the portal and you should avoid using "local.settings.json" file because it is only for local development:
I hope this will help.
If your intention is to use the key Connectionstring from the local.settings.json, you should be able to get the value from there automatically just by doing this:
public static void Run([ServiceBusTrigger("topic", "FunctionTopicMessageLogger", Connection = "connectionString")]string mySbMsg)
The value for Connection is the name of the Key from the local.settings.json file.
If you don't have a lot of different Service Bus, you can create one function for each different connectionString you have.
Each function needs to know the connectionString before starting, and start waiting for message in the service bus.
If you have to do that dynamically, you don't have to use an Azure function, you can use the Service Bus SDK:
Here you can see how to manage a client manually: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues
I hope this will help you!

Load Connection String from Config File in Azure Functions

In my Azure Function I am using a Library which establishes a connection to an SQL server via the ConnectionString from the ConfigurationManager like this:
var cs = System.Configuration.ConfigurationManager.ConnectionStrings["DbConString"].ConnectionString;
DbConnection connection = new SqlConnection(cs);
Now when i set the connection string DbConString in the portal via the Application Settings everything is working fine. But for local development I use the azure-functions-cli and unfortunately I have no idea where i should place the connection string to have it loaded correctly via the ConfigurationManager.
I've tried to place it in the appsettings.json file but without success.
Edit:
My appsettings.json currently looks like this:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": "",
"MyServiceBusReader": "Endpoint=sb://xxxx=",
"DbConStr1": "data source=(localdb)\\MSSQLLocalDB;initial catalog=MyDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework",
"ConnectionStrings": {
"DbConStr2": "data source=(localdb)\\MS..."
}
}
}
But I am not able to access "DbConStr1" via the ConfigurationManager.
Adding "DbConStr2" within "ConnectionStrings" like described here leads to a compilation error. Maybe because I am not using .NET Core?
Edit2:
I messed up the nesting of "ConnectionStrings". It has to be on the same nesting level as "Values":
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": "",
"MyServiceBusReader": "Endpoint=sb://xxxx="
},
"ConnectionStrings": {
"DbConStr": "data source=(localdb)\\MS..."
}
}
Add file local.setting.json
{
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"tenantId": "You tenantId",
"resource": "https://management.azure.com/",
"ClientSecret": "You ClientSecret, Key from App Registry",
"ClientId": "You ClientId, Application ID from App registry",
"subscriptionId": "You subscriptionId",
"resourceGroupName": "Your resourceGroupName",
"serverName": " Your SQL Server",
"databaseNameDW": "Your Database",
"apiversion": "2017-10-01-preview"
}
}
In C# Code use:
private readonly static string tenantId = ConfigurationManager.AppSettings["tenantId"];
// C# Environment Variables example for Azure Functions v1 or v2 runtime
// This works all the way up to but not including .NET Core 2.0
var clientId = Environment.GetEnvironmentVariable("ClientId");
var clientSecret = Environment.GetEnvironmentVariable("ClientSecret");
var aadDomain = Environment.GetEnvironmentVariable("AADDomain");
Please do remember the settings you do in local.settings.json will not be reflected in azure. Please add your values in app setting from Azure portal follow the link-
https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings
I had this same issue and am using .net standard (as opposed to core). I added my settings to the Application Settings section of my Azure function (in Azure Portal):-
I then downloaded a zip of the function:-
Included in this download is a copy of local.settings.json that includes my app settings in the correct json format. I then access them via ConfigurationManager.Appsettings["mysetting"]
The problem was, that a connection string known from e.g. a Web.config file consists of two parts:
The connection string itself and
the provider name.
But since the configuration file uses the JSON format it was not possible to specify both parameters.
At the time when the question was asked, it was not possible to set the provider name in the appsetings.json (now renamed to local.settings.json). But the Azure-Functions-team change this and set a default value for providerName to System.Data.SqlClient, which solved the problem.
The providerName defaults to System.Data.SqlClient. You don't have to set it manually. Just add your connection string X and read it via ConfigurationManager.ConnectionStrings["X"].
I've found a method which feels hacky, but works: if you do evaluate Environment.GetEnvironmentVariables() you can spot that all Connection Strings from your local.settings.json are available as Environment Variables with a "ConnectionStrings:" prefix if run locally, or one from several "xxxCONNSTR_" if running on Azure, so you can define this helper function:
private static Array ConnectionStringKeyPrefixes = new[] { "ConnectionStrings:", "SQLCONNSTR_", "SQLAZURECONNSTR_", "MYSQLCONNSTR_", "POSTGRESQLCONNSTR_", "CUSTOMCONNSTR_" };
public static string GetConnectionStringFromSettings(string connectionStringName)
{
string connectionString = null;
foreach(string prefix in ConnectionStringKeyPrefixes) {
connectionString = Environment.GetEnvironmentVariable($"{prefix}{connectionStringName}");
if (!string.IsNullOrWhiteSpace(connectionString))
{
break;
}
}
return connectionString;
}
You should be able to manage your configuration settings with an appsettings.json file in your project structure. You can take a look here for an example of the folder structure for Azure Functions.
Additionally, this link will have some details about how to manage configuration settings with .NET Core.

Setting the SQL connection string for ASP.NET Core web app in Azure

I have created a new ASP.NET Core web application in Visual Studio 2015. I've also set up an Azure web app to pull in the app from GitHub and run it. This works fine, but I'm having trouble connecting to the database on Azure.
Locally, this works, and it uses config.json and in code Data:DefaultConnection:ConnectionString for the connection string.
How can I leave the code as it is, and have it work in Azure too? I've tried setting the Application Settings in the portal, both the Connection Strings and the App Settings. And using both "SQLCONNSTR_DefaultConnection" and "Data:DefaultConnection:ConnectionString" as the key.
(Setting the App Settings doesn't seem to work by the way. I think the value I provide is too long).
So how can I provide the connection string for my Azure database to my Azure web app (ASP.NET 5), without checking it in in source control?
Update
My Startup.cs looks like this (see the full file on GitHub):
public Startup(IHostingEnvironment env)
{
var configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
configuration.AddUserSecrets();
}
configuration.AddEnvironmentVariables();
Configuration = configuration;
}
In the ConfigureServices method, there is also:
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
and, also in the ConfigureServices method:
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
.AddDbContext<InvoicesDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// So this is where I want my app in Azure to use the connection string I
// provide in the portal
Short answer
I've tried setting the Application Settings in the portal, both the Connection Strings and the App Settings. And using both "SQLCONNSTR_DefaultConnection" and "Data:DefaultConnection:ConnectionString" as the key.
You're close.
Go to Azure web app > configure > connection strings.
Add a connection string with the name DefaultConnection.
Use Configuration.Get("Data:DefaultConnection:ConnectionString") to access it.
Example using timesheet_db instead of DefaultConnection
This is an example from my own timesheet application. My connection string was named timesheet_db. Just replace all instances of that string with DefaultConnection to adapt the example to your use case.
Azure web app configuration
Azure web app service control manager
The online service control manager at https://myWebAppName.scm.azurewebsites.net/Env will show the connection strings.
Startup.cs
Setup configuration settings in Startup so that the environmental variables overwrite the config.json.
public IConfiguration Configuration { get; set; }
public Startup()
{
Configuration = new Configuration()
.AddJsonFile("config.json")
.AddEnvironmentVariables(); <----- will cascade over config.json
}
Configure the database in Startup.
public void ConfigureServices(IServiceCollection services)
{
services
.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ProjectContext>(options =>
{
var connString =
Configuration.Get("Data:timesheet_db:ConnectionString");
options.UseSqlServer(connString);
});
}
Of course, the example uses a connection string named timesheet_db. For you, replace all instances of it with your own connection string named DefaultConnection and everything will work.
In RC2 I had to change how my connection strings were read to get them to work in Azure. In my case I had to ensure the Azure connection string was named "DefaultConnection", and accessed by:
RC1:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
}
}
}
Accessed by:
var conn = Configuration["Data:DefaultConnection:ConnectionString"];
RC2:
{
"Data": {
},
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
}
}
Accessed by:
var conn = Configuration.GetConnectionString("DefaultConnection");
You have a number of options to set your connection string.
The default setup class get the enviroment settings from different sources.
You could set your connection string in config.production.json. or config.staging.json. See the startup class
public Startup(IHostingEnvironment env)
{
// Setup configuration sources.
var configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This reads the configuration keys from the secret store.
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
configuration.AddUserSecrets();
}
configuration.AddEnvironmentVariables();
Configuration = configuration;
}
I think you are looking for SlotSticky Settings
Use this command in Azure PowerShell to set 2 app settings as sticky to the slot
Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames #("myslot", "myslot2")
And this command to set 2 connection strings as sticky to the slot
Set-AzureWebsite -Name mysite -SlotStickyConnectionStringNames #("myconn", "myconn2")

Categories