Search code examples
c#azure-functionsazure-functions-core-tools

Cannot read connection string defined in local.settings.json


Basically, I can get my code to load connection string from local.settings.json.

I use Visual Studio 2019 to develop HTTP triggered Azure Functions (v3) locally. I defined a Startup class that initializes and injects a CosmosDB client so that all functions can reference it (as shown below). Before instantiating CosmosDB client, ConfigurationManager is used to get a connection string for CosmosDB.

using System.Configuration;
using AttendanceTaking.Infra.CosmosDB;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(Hello.Startup))]
namespace Hello
{
    class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton((service) =>
           {
               var connectionString = getConnectionString("CosmosDB");
               var cosmosClientBuilder = new CosmosClientBuilder(connectionString);
               return cosmosClientBuilder.Build();
           });
        }

        private string getConnectionString(string configName)
        {
            return ConfigurationManager.ConnectionStrings[configName].ConnectionString;
        }
    }
}

The connection string itself is defined in local.settings.json as show below.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  },
  "ConnectionStrings": {
    "CosmosDB": "AccountEndpoint=account-endpoint-secret;"
  }
}

Then I run my function and whenever I send a request to the HTTP endpoint, an exception is thrown at ConfigurationManager.ConnectionStrings[configName].ConnectionString;, saying,

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.

What am I doing wrongly here?


Solution

  • You can use two methods here.

    1. Environment variables

    I think you can get the connection string with the Environment variables, because you can call System.Environment.GetEnvironmentVariable to get the connection string both when developing locally and when running in Azure.

    The method of obtaining environment variables is as follows:

        public static string GetEnvironmentVariable(string name)
        {
            return name + ": " +
                System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
        }
    

    Then you can use this format to get the value of the environment variable:

    GetEnvironmentVariable("ConnectionStrings:CosmosDB");
    

    For more details, you can refer to the official documentation.

    1. ASP.NET Core Configuration

    As Chris mentioned in the comments, you can use ASP.NET Core Configuration. For more information, you can refer to this official document