ayepiz ayepiz - 1 year ago 80
C# Question

Why an azure function doesn't see the connection strings configured?

For some strange reason my azure function stopped seeing the connection strings from the app configuration.

I can assure that this code was working before, after it worked I continue to add simple validations in the main function and at the end it stopped finding the connection strings configured.

The problem right now is with the GetMetaData function, when accessing mongo:cs connection string an exception is raised with error Object reference not set to an instance of an object, which means that connection string mongo:cs was not found.

I added some debug code to the function in order to see which connection strings get loaded and found out that the ones loaded doesn't correspond to the ones configured.

Does anyone has faced a similar problem?

You can find the complete function code, the output and an image with the connection strings configured

Runtime version: latest (~1)

using MongoDB.Bson;
using MongoDB.Driver;
using System.Configuration;
using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

// parse query parameter
string sId = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "id", true) == 0)

Guid id;
if(Guid.TryParse(sId, out id))
string metaData = GetMetaData(id, log);
return req.CreateResponse(HttpStatusCode.NotFound);
return req.CreateResponse(HttpStatusCode.OK, metaData);

return req.CreateResponse(HttpStatusCode.BadRequest);

private static string GetMetaData(Guid id, TraceWriter log)
//Begin debug code that was added after the connection string stopped working
var count = ConfigurationManager.ConnectionStrings.Count;
var name0 = ConfigurationManager.ConnectionStrings[0].Name;
var name1 = ConfigurationManager.ConnectionStrings[1].Name;
//End debug code
string cnn = ConfigurationManager.ConnectionStrings["mongo:cs"].ConnectionString;
IMongoClient client = new MongoClient(cnn);
IMongoDatabase db = client.GetDatabase("cxsopdev");
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var result = db.GetCollection<BsonDocument>("excelfiletoload").Find(filter).FirstOrDefault();
return result.ToString();

2016-11-12T00:27:56.786 Function started (Id=20c0183f-9989-4a62-9834-461df7fd8ae5)
2016-11-12T00:27:56.786 C# HTTP trigger function processed a request. RequestUri={...}
2016-11-12T00:27:56.786 Step1=857B689F-C32B-46DA-BCD9-05D92A0F151A
2016-11-12T00:27:56.786 Step1.5=857b689f-c32b-46da-bcd9-05d92a0f151a
2016-11-12T00:27:56.786 Step6
2016-11-12T00:27:56.786 Count=2
2016-11-12T00:27:56.786 Name0=LocalSqlServer
2016-11-12T00:27:56.786 Name1=LocalMySqlServer
2016-11-12T00:27:56.786 Function completed (Failure, Id=20c0183f-9989-4a62-9834-461df7fd8ae5)
2016-11-12T00:27:56.786 Exception while executing function: Functions.GetImportMetaData. GetImportMetaData: Object reference not set to an instance of an object.

Connection strings configured

Answer Source

You found a bug!

Please change FUNCTIONS_EXTENSION_VERSION app setting back to ~0.9 for now. We'll hopefully update ~1 with a hotfix soon. I'll try to remember to update this once we've confirmed it's fixed.

Also created an issue to make sure we test ConfigurationManager properly working from now on... https://github.com/Azure/azure-webjobs-sdk-templates/issues/353

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download