我想使用 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 文件
关于azure - 从 Azure Function .csx 查询 CosmosDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49148030/