Load Connection String from Config File in Azure F

2019-02-23 02:24发布

问题:

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..." 
  }
}

回答1:

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"];


回答2:

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"]



回答3:

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.



回答4:

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"].