0

We have a referenced project in azure function app project. The referenced assembly is a data service project which is referred to by web api project too.

When referenced in web-api project the data service project automatically refers to web.config file for connection strings and app settings. While in azure functions app the data service project is not able to locate the connection strings stored in local.settings.json file.

  1. How to address this issue locally?
  2. How to address the issue in production?

NOTE: Would like to have DRY approach here.

Shyamal Parikh
  • 2,988
  • 4
  • 37
  • 78
  • How do you read the connection string? – Mikhail Shilkov Jan 17 '18 at 08:44
  • I access app settings currently as ConfigurationManager.AppSetting("ss1ConnString") – Shyamal Parikh Jan 17 '18 at 10:10
  • Shouldn't it be `ConfigurationManager.ConnectionStrings["ss1ConnString"].ConnectionString`? – Mikhail Shilkov Jan 17 '18 at 10:16
  • Ya, that's right. It is working with `web-api` project but not with `azure functions app` – Shyamal Parikh Jan 17 '18 at 10:41
  • How exactly are you testing? Are you running the function in the actual function host runtime (e.g. F5 in Visual Studio or )?? What does your `local.settings.json` look like? – Drew Marsh Jan 18 '18 at 01:24
  • 1
    Yes, please post the contents of the `local.settings.json` file as this should 'just work'. Do remember the `ConnectionStrings` should be placed in a different block as the `AppSettings`. Refer to this answer: https://stackoverflow.com/a/45555781/352640 – Jan_V Jan 18 '18 at 12:48

1 Answers1

0

As Jan V said, you could add a data connection string in json file. Besides, you could set a break point to see whether you get the 'str' value (Debug).

var str = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

Code in local.settings.jons file:

 {
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": 
   "your storage account connection string",
    "AzureWebJobsDashboard": 
"your storage account connection string"

  },
  "ConnectionStrings": {
"ConnectionStringName": "Data Source=tcp:database server name,1433;Initial Catalog=database name;Integrated Security=False;User Id=user name;Password= your Password;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True"  // Refer to Azure portal>SQL database> connection string
}
}

For more details about how to use Azure Functions to connect to an Azure SQL Database ,you could read this article.

Janley Zhang
  • 1,567
  • 7
  • 11