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

标签 c# azure-functions azure-cosmosdb

我无法弄清楚如何在 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/

相关文章:

c# - 使用 LINQ 从 xml 中删除元素

node.js - 如何将 Azure Httptrigger 连接到网站?

azure - 计时器触发功能应用程序缺少几次运行

azure - 在子对象的查询中返回重复项

使用 local.settings.json 的 Azure 函数 cosmos db 输出

c# - Entity Framework Core LINQ 类属性/方法不能使用包含的属性

c# - 如何调试通过 Assembly.Load(byte[]) 加载的程序集?

c# - 如何在 Visual Studio 中打开特殊的 Ascii 字符?

Azure Functions 方法未找到错误

azure - Microsoft.Azure.Cosmos.Table - 如果使用 insertOrMergeOperation 插入或合并项目,如何检索项目操作状态?