gpt4 book ai didi

c# - 如何通过输入和输出绑定(bind)连接 Azure Functions 2.0 CosmosDB?

转载 作者:行者123 更新时间:2023-12-03 17:49:53 26 4
gpt4 key购买 nike

我无法弄清楚如何在 Azure Function 2.0 中同时使用 in 和 out 绑定(bind)访问 CosmosDB。

我可以从一个 HttpTrigger 函数从我的 cosmosDB 集合和另一个 HttpTrigger 函数中获取一个 json 对象,将 json 对象写入集合

我想不通的是如何首先从 cosmosDB 集合中读取 json 对象,对其进行一些更改,然后从同一个函数中再次将其写回。

下面的代码应该概述我的问题

[FunctionName("WebrootConnector")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection",
CreateIfNotExists = true,
Id = "999",
PartitionKey = "/id")]
Customers customersObject, // in binding
out dynamic customersDocumentToDB, // out binding
ILogger log)
{
// Chect if a customersObject is recieved from cosmosDB
if (customersObject == null)
{
// Create a new Customers object
customersObject = new Customers();
// Set the id of the database document (should always be the same)
customersObject.Id = 999;
// Create a new empty customer list on the customers object
customersObject.customers = new List<Customer>();

// Add some customers to the list

}
else
{
// if a object is received from the database
// do something with it.
}

if (customersObject.customers != null)
{
// Write the object back to the cosmosDB collection
customersDocumentToDB = customersObject;
log.LogInformation($"Data written to customerDB");
}
else
{
customersDocumentToDB = null;
log.LogInformation($"Nothing to write to database");
}
}

最佳答案

您必须使用 两个单独的绑定(bind) ,一个用于输入(您的查询),一个用于输出。完整列表位于 official docs for the Bindings .

[FunctionName("WebrootConnector")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection",
CreateIfNotExists = true,
Id = "999",
PartitionKey = "/id")]
Customers customersObject, // in binding
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection"]
out dynamic customersDocumentToDB, // out binding
ILogger log)

如果要存储多于 1 个文档,可以使用 IAsyncCollector :
[FunctionName("WebrootConnector")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection",
CreateIfNotExists = true,
Id = "999",
PartitionKey = "/id")]
Customers customersObject, // in binding
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection"]
IAsyncCollector<dynamic> customersDocumentToDB, // out binding
ILogger log)

当您想保存文档时,请调用 await customersDocumentToDB.AddAsync(newDocument) .

关于c# - 如何通过输入和输出绑定(bind)连接 Azure Functions 2.0 CosmosDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54423044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com