【问题标题】:Query CosmosDB from Azure Function .csx从 Azure Function .csx 查询 CosmosDB
【发布时间】:2018-08-15 07:49:21
【问题描述】:

我想使用 csx 查询 CosmosDB 集合以查看 Azure 函数中是否已存在文档。

除了以下代码之外,我还对 cosmosDB 集合进行了隐式绑定,以便能够创建新文档。这是使用

binder.BindAsync<IAsyncCollector<string>>(new CosmosDBAttribute("test", "collection")

这是我的函数的简单版本。

#r "System"
#r "Microsoft.Azure.WebJobs.Extensions.CosmosDB"

using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

public static async Task<string> Run(string localityId, Binder binder, TraceWriter log)
{
    ...

    string EndpointUrl = "<your endpoint URL>";
    string PrimaryKey = "<your primary key>";
    DocumentClient client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);

    ...
}

这会导致以下错误消息:

错误 CS0246:找不到类型或命名空间名称“DocumentClient”(您是否缺少 using 指令或程序集引用?)

我已经安装了扩展Microsoft.Azure.WebJobs.Extensions.CosmosDB

我在 MacOS 上运行,使用 func host start 命令在本地进行测试。

【问题讨论】:

    标签: azure azure-cosmosdb azure-functions document-database csx


    【解决方案1】:

    错误 CS0246:找不到类型或命名空间名称“DocumentClient”(您是否缺少 using 指令或程序集引用?)

    看来你需要参考#r "Microsoft.Azure.Documents.Client"。您也可以从Azure Cosmos DB bindings for Azure Functions获取演示代码

     #r "Microsoft.Azure.Documents.Client"
    
        using System;
        using Microsoft.Azure.Documents;
        using System.Collections.Generic;
    
    
        public static void Run(IReadOnlyList<Document> documents, TraceWriter log)
        {
          log.Verbose("Documents modified " + documents.Count);
          log.Verbose("First document Id " + documents[0].Id);
        }
    

    更新:

    若要在 C# 函数中使用 NuGet 包,请将 project.json 文件上传到函数应用文件系统中的函数文件夹。这是一个添加引用的示例 project.json 文件

    【讨论】:

    • 这不起作用。参考 #r "Microsoft.Azure.Documents.Client" 给出了错误:Compilation service error [03/07/2018 09:39:25] Microsoft.Azure.WebJobs.Script: C# compilation service error: Could not load file or assembly 'Microsoft.Azure.Documents.Client, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. [03/07/2018 09:39:25] . System.Private.CoreLib: Could not load file or assembly 'Microsoft.Azure.Documents.Client, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
    • 还是同样的错误。我在本地运行,是否需要运行安装命令或类似命令?
    • 如果是在本地运行,和azure portal是不一样的。它是一个预编译的天蓝色函数。您需要将Microsoft.Azure.DocumentDB 安装到您的函数中
    • 我尝试在函数文件夹中运行nuget install Microsoft.Azure.DocumentDB。这下载了包,但我的函数仍然返回相同的错误。 The type or namespace name 'Documents' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
    • 如果有快照会更有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多