azure - 从 Azure Function .csx 查询 CosmosDB

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

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

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

完成的
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);

    ...
}

这会导致以下错误消息:

error CS0246: The type or namespace name 'DocumentClient' could not be found (are you missing a using directive or an assembly reference?)

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

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

最佳答案

error CS0246: The type or namespace name 'DocumentClient' could not be found (are you missing a using directive or an assembly reference?)

看来您需要引用#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 文件

enter image description here

关于azure - 从 Azure Function .csx 查询 CosmosDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49148030/

相关文章:

azure - 此定义需要位置属性

Azure CDN 默认文档index.html

azure - 命名空间 'Microsoft.Azure.WebJobs' 中不存在 EventHubTriggerAttribute

sql-server - 性能 Azure 弹性池数据库

.net-core - 如何在 dotnet 核心应用程序中将 DocumentDB 客户端初始化为单例

azure-cosmosdb - 在 Azure CosmosDb PatchItemAsync 中获取 "One of the specified inputs is invalid"

AKS 中的 Azure Functions HttpTrigger 键

如果我想在我的nodejs函数中调用Linux可执行库,Azure函数: do I have to use Premium plan's custom linux image feature,?

node.js - 使用nodejs将数据从Rfid rc522发送到azure iot hub

c# - 在服务器端同时进行排序和分页的 DocumentDb 查询,可能吗?