【问题标题】:Azure Function DocumentClient BindingAzure 函数 DocumentClient 绑定
【发布时间】:2018-03-30 12:19:20
【问题描述】:

我正在尝试将我的 DocumentDB nuget 包从 1.13 升级到 1.18

我在升级具有 DocumentClient 绑定的 azure 函数时遇到问题。

在 DocumentDB 1.13 中,绑定部分不采用 :{Id} 作为绑定参数,而是完美地创建了 DocumentClient 对象。而 DocumentDB 1.18 需要 {Id} 作为绑定参数 [我不想要,因为我想遍历集合中的整个文档]

我在 1.18 之前的 host.json 绑定是

{
 "frameworks": {
 "net46": {
 "dependencies": {
 "Dynamitey": "1.0.2",
 "Microsoft.Azure.DocumentDB": "1.13.0",
 "Microsoft.Azure.WebJobs.Extensions.DocumentDB": "1.0.0"
}
 }
}

我的 local.settings.json 只有

{
"IsEncrypted": false,
"Values": {
 "AzureWebJobsStorage": " 
 DefaultEndpointsProtocol=xxxxx/xxxxx==;EndpointSuffix=core.windows.net",
 "AzureWebJobsDashboard": "",
 "AzureWebJobsDocumentDBConnectionString": 
 "AccountEndpoint=xxxxx/;AccountKey=xxxx==;",
 }
}

我的天蓝色函数看起来像

 [FunctionName("DeleteAVFeedAuditData")]
    public static async Task Run([TimerTrigger("0 0/1 * * * *")]TimerInfo myTimer,  [DocumentDB]DocumentClient client,
    TraceWriter log)
{

    var c = client;
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    var value=ConfigurationManager.AppSettings["AVAuditFlushAfterDays"];

    var collectionUri = UriFactory.CreateDocumentCollectionUri("AVFeedAudit", "AuditRecords");
    //var documents = client.CreateDocumentQuery(collectionUri,"Select * from c where c.EndedAt");

    //foreach (Document d in documents)
    //{
    //    await client.DeleteDocumentAsync(d.SelfLink);
    //}

}

}

现在,当使用更新的 documentDB 1.18 包运行 azure 函数时,它会说绑定 {Id} ,这将只给出具有指定 id 的单个文档。而我的要求与以前版本的 DocumentDB 1.13 相同。

请告诉我如何使用新的更新包获取与我的 DocumentClient 绑定的整个文档。

【问题讨论】:

    标签: azure azure-cosmosdb azure-functions serverless-framework serverless


    【解决方案1】:

    根据您的描述,我检查了这个问题,并将这个问题重现如下:

    请告诉我如何使用新的更新包获取与我的 DocumentClient 绑定的整个文档。

    根据您的情况,我建议您自己构建DocumentClient,而不是使用绑定到DocumentClient 来实现您的目的。

    DocumentClient client = new DocumentClient(new Uri("https://<your-account-name>.documents.azure.com:443/"), "<your-account-key>");
    

    您可以在local.settings.json 文件下配置serviceEndpointaccountKey,就像应用程序设置AzureWebJobsStorage 一样。然后您可以使用以下代码来检索您的设置值:

    ConfigurationManager.AppSettings["your-appsetting-key"];
    

    另外,这里有一个issue关于从连接字符串构造DocumentClient,你可以参考一下。

    更新:

    对于 1.18,以下代码可以按预期工作:

    [FunctionName("Function1")]
    public static void Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, [DocumentDB("brucedb01", "brucecoll01",ConnectionStringSetting = "AzureWebJobsDocumentDBConnectionString")] IEnumerable<dynamic> documents, TraceWriter log)
    {
        foreach (JObject doc in documents)
        {
            //doc.SelectToken("_self").Value<string>();
            log.Info(doc.ToString());
        }
    }
    

    【讨论】:

    • 嗨 Bruce,由于 azure 函数允许我们直接绑定 documentclient,所以不想显式创建 documentclient 对象。在 documentDB 的 v. 1.13 中,它可以很好地与 binding 配合使用,但在 1.18 中它失败了,因为它要求我使用设置中配置的 id 绑定单个文档。我的要求是绑定没有id的documentclient。
    • 我更新了一些研究的答案,你可以参考一下。此外,我假设有一个错误,您可以添加您的问题here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2018-07-05
    相关资源
    最近更新 更多