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

时间:2016-11-12 00:35:58

标签: c# azure azure-functions

由于某些奇怪的原因,我的azure函数停止从应用程序配置中看到连接字符串。

我可以确保此代码之前有效,在它工作之后我继续在main函数中添加简单验证,最后它停止查找已配置的连接字符串。

现在的问题是使用GetMetaData函数,当访问mongo:cs连接字符串时会引发异常,错误对象引用未设置为对象的实例,这意味着找不到连接字符串mongo:cs。 / p>

我在函数中添加了一些调试代码,以便查看加载了哪些连接字符串,并发现加载的连接字符串与配置的字符串不对应。

有没有人遇到过类似的问题?

您可以找到完整的功能代码,输出和配置了连接字符串的图像

运行时版本:最新(~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

1 个答案:

答案 0 :(得分:2)

你发现了一个错误!

现在请将FUNCTIONS_EXTENSION_VERSION应用设置更改回~0.9。我们希望很快就能通过修补程序更新~1。一旦我们确认它已修复,我会记得更新此内容。

还创建了一个问题,以确保我们从现在开始正确地测试ConfigurationManager ... https://github.com/Azure/azure-webjobs-sdk-templates/issues/353