ayepiz ayepiz - 28 days ago 6
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)
.Value;

Guid id;
log.Info($"Step1={sId}");
if(Guid.TryParse(sId, out id))
{
log.Info($"Step1.5={id}");
string metaData = GetMetaData(id, log);
log.Info($"Step2");
if(String.IsNullOrEmpty(metaData))
{
log.Info($"Step3");
return req.CreateResponse(HttpStatusCode.NotFound);
}
else
{
log.Info($"Step4");
return req.CreateResponse(HttpStatusCode.OK, metaData);
}

}
else
{
log.Info($"Step5");
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
log.Info($"Step6");
var count = ConfigurationManager.ConnectionStrings.Count;
log.Info($"Count={count}");
var name0 = ConfigurationManager.ConnectionStrings[0].Name;
log.Info($"Name0={name0}");
var name1 = ConfigurationManager.ConnectionStrings[1].Name;
log.Info($"Name1={name1}");
//End debug code
string cnn = ConfigurationManager.ConnectionStrings["mongo:cs"].ConnectionString;
log.Info($"Step7");
IMongoClient client = new MongoClient(cnn);
log.Info($"Step8");
IMongoDatabase db = client.GetDatabase("cxsopdev");
log.Info($"Step9");
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
log.Info($"Step10");
var result = db.GetCollection<BsonDocument>("excelfiletoload").Find(filter).FirstOrDefault();
log.Info($"Step11");
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

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