gpt4 book ai didi

c# - 为什么 azure 函数看不到配置的连接字符串?

转载 作者:行者123 更新时间:2023-11-30 23:18:54 25 4
gpt4 key购买 nike

由于某种奇怪的原因,我的 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

关于c# - 为什么 azure 函数看不到配置的连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com