【问题标题】:Send SqlQuery in Azure Function's DocumentDB Attribute在 Azure Functions DocumentDB 属性中发送 Sql 查询
【发布时间】:2017-08-02 21:23:23
【问题描述】:

我有一个使用 DocumentDB 属性连接到 Cosmos DB 的 Azure 函数。我正在使用 Azure Functions for Visual Studio 2017 工具。这是简单的函数

    [FunctionName("DocumentDbGet")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
        [DocumentDB("FunctionJunctionDemo", "Demo")]IEnumerable<DemoModel> items)
    {
        //Can perform operations on the "items" variable, which will be the result of the table/collection you specify
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        var item = items.Where(x => x.FirstName == name);
        return req.CreateResponse(HttpStatusCode.OK);
    }

我希望能够将SqlQuery 作为DocumentDB 属性的参数之一传递,如下所示:

    [FunctionName("DocumentDbGet")]
    public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
        [DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = $"select * from c where c.Id = {Id}")]IEnumerable<DemoModel> items)
    {
        //Can perform operations on the "items" variable, which will be the result of the table/collection you specify
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        var item = items.Where(x => x.FirstName == name);
        return req.CreateResponse(HttpStatusCode.OK);
    }

我只看到 1 个示例执行此操作,并报告说它应该有效。 https://github.com/Azure/Azure-Functions/issues/271 我收到的“错误”是它没有将任何名为 SqlQuery 的东西识别为可能的参数。

我查看了 Azure 函数输入绑定的文档 https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents 显示 function.json 文件中包含 sqlQuery 属性的输出。那是怎么进去的?

如果无法在 DocumentDB 属性中传入 SqlQuery,那么预先过滤结果以避免返回整个集合然后通过 LINQ 查询运行它的最佳做法是什么?

【问题讨论】:

    标签: azure azure-cosmosdb azure-functions


    【解决方案1】:

    您需要引用1.1.0-beta 版本的Microsoft.Azure.WebJobs.Extensions.DocumentDB NuGet 包(或更高版本)。

    在那个版本中,SqlQueryDocumentDB 属性的有效参数。如果我在select 字符串之前删除$ 符号,您的代码将为我编译:

    [DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]
    

    您不需要$ - 它用于 C# 中的字符串插值,而不是您想要在这里做的事情。

    【讨论】:

    • 嗨@Mikhail - 感谢您的快速回答!因此,即使我删除了从一开始就绝对不需要的$,它仍然无法识别它是有效的。也许我的版本不匹配?我正在使用 Microsoft.Azure.WebJobs.Extensions.DocumentDB v1.0.0,这是 DocumentDB 属性所在的位置。也许我需要更新我的 Azure CLI?
    • 所以我将 Azure Functions CLI 更新到最新版本,并将我的 Visual Studio 2017 Preview 更新到 Preview 7,然后我可以将 Visual Studio 的 Azure Function 工具从 0.2 更新到 0.3。 30802。这些都没有帮助。在我的机器上,SqlQuery 下仍然有很棒的红色波浪线。也许我完全错过了一个包裹?
    • @EricFleming 我引用的是1.1.0-beta1 版本的...Extensions.DocumentDB 包。这会有所作为吗?
    • 啊哈! - @Mikhail 就是这样。我没有在 NuGet 包管理器中选中“包括预发布”复选框。检查后,我看到了...Extensions.DocumentDB 包的1.1.0-beta1。现在看起来它接受SqlQuery 作为参数。您能否在上面编辑您的答案以包括我们至少需要使用此软件包的 1.1.0-beta 版本才能使用 SqlQuery 参数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多