Pepozzo
Pepozzo

Reputation: 229

Azure function can't connect to Azure SQL Database

I've created an Azure function that should connect to an Azure SQL database.

This function retrieves the connection string saved in the application config. This connection string is the one provided from Azure SQL.

Everything works fine on my PC, but I've deployed the function on Azure and it seems unable to connect to the SQL database (returning the common error "object reference not set...").

I've checked possibleOutboundIpAddresses in Azure function resources and I've allowed them on the Azure SQL firewall.

Also, my SQL database is in a Azure VNet with a storage.

Any idea?

Upvotes: 2

Views: 2956

Answers (2)

rickvdbosch
rickvdbosch

Reputation: 15551

You should probably add the connection string you need to the App Settings of the Function, not a configuration file. Unless you specifically add the configuration file to be one of the locations to look for settings using the ConfigurationBuilder, it will not be picked up by default.

More information: Azure Functions C# developer reference

App settings can be read from environment variables both when developing locally and when running in Azure. When developing locally, app settings come from the Values collection in the local.settings.json file. In both environments, local and Azure, GetEnvironmentVariable("<app setting name>") retrieves the value of the named app setting. For instance, when you're running locally, "My Site Name" would be returned if your local.settings.json file contains { "Values": { "WEBSITE_SITE_NAME": "My Site Name" } }.

Upvotes: 7

Jarnstrom
Jarnstrom

Reputation: 477

There is a similar thread here

In function runtime v2, you should use Environment.GetEnvironmentVariable("string_name",EnvironmentVariableTarget.Process) to get values from Application settings and connection strings.

Maybe this applies to your setup as well.

Upvotes: 0

Related Questions