2

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?

2
  • 3
    ConfigurationManager isn't supported anymore. Use asp.net core config. blog.jongallant.com/2018/01/azure-function-config Commented Jul 6, 2020 at 22:40
  • Omg... thank you so much... Commented Jul 6, 2020 at 23:16

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.