为什么 azure 函数看不到配置的连接字符串?
Why an azure function doesn't see the connection strings configured?
由于某些奇怪的原因,我的 azure 函数停止查看来自应用程序配置的连接字符串。
我可以保证这段代码之前是有效的,在它有效之后我继续在主函数中添加简单的验证,最后它停止查找配置的连接字符串。
现在的问题是 GetMetaData 函数,在访问 mongo:cs 连接字符串时引发异常,错误为对象引用未设置为对象的实例,这意味着连接字符串 mongo:cs 没有找到。
我在函数中添加了一些调试代码以查看加载了哪些连接字符串,发现加载的与配置的不对应。
有没有人遇到过类似的问题?
您可以找到完整的函数代码、输出和配置连接字符串的图像
运行时版本:最新 (~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
您发现了一个错误!
请暂时将 FUNCTIONS_EXTENSION_VERSION
应用设置改回 ~0.9。我们希望尽快用修补程序更新 ~1。一旦我们确认它已修复,我会尽量记得更新它。
还创建了一个问题以确保我们测试 ConfigurationManager 从现在开始正常工作...https://github.com/Azure/azure-webjobs-sdk-templates/issues/353
由于某些奇怪的原因,我的 azure 函数停止查看来自应用程序配置的连接字符串。
我可以保证这段代码之前是有效的,在它有效之后我继续在主函数中添加简单的验证,最后它停止查找配置的连接字符串。
现在的问题是 GetMetaData 函数,在访问 mongo:cs 连接字符串时引发异常,错误为对象引用未设置为对象的实例,这意味着连接字符串 mongo:cs 没有找到。
我在函数中添加了一些调试代码以查看加载了哪些连接字符串,发现加载的与配置的不对应。
有没有人遇到过类似的问题?
您可以找到完整的函数代码、输出和配置连接字符串的图像
运行时版本:最新 (~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
您发现了一个错误!
请暂时将 FUNCTIONS_EXTENSION_VERSION
应用设置改回 ~0.9。我们希望尽快用修补程序更新 ~1。一旦我们确认它已修复,我会尽量记得更新它。
还创建了一个问题以确保我们测试 ConfigurationManager 从现在开始正常工作...https://github.com/Azure/azure-webjobs-sdk-templates/issues/353