如何通过输入和输出绑定连接Azure Functions 2.0 CosmosDB?

时间:2019-01-29 14:17:51

标签: c# azure-functions azure-cosmosdb

我在弄清楚如何在Azure Function 2.0中同时使用in和out绑定访问CosmosDB时遇到麻烦。

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

我不知道如何从同一个Function内首先从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 allways 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");
            }
         }

2 个答案:

答案 0 :(得分:4)

您必须使用两个独立的绑定,一个在(查询),一个出。完整列表在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)

如果要存储多个文档,可以使用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)

答案 1 :(得分:1)

如果其他人也有相同的问题,仅供以后参考

这对我有用。

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

            } 
            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");
            }
         }

Customers类:

public class Customers
    {
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("lastUpdated")]
        public System.DateTime lastUpdated { get; set; }
        [JsonProperty("customers")]
        public List<Customer> customers { get; set; }
    }

public class Customer
    {
        [JsonProperty("customerId")]
        public int customerID { get; set; }
        [JsonProperty("customerName")]
        public string customerName { get; set; }
        [JsonProperty("customerKeycode")]
        public string customerKeyCode { get; set; }
    }

添加绑定后,一个用于输入,一个用于输出,并将我的customerObject类id参数更改为string而不是int,一切工作正常,除了in绑定始终返回customersObject = null,即使我有一个文档也是如此。由out绑定创建的id =“ 999”的集合。

我发现对我来说,解决方案是删除Azure门户上cosmosDB中的集合,并向out绑定添加CreateIfNotExists = true。这样,out绑定就可以创建没有PartitionKey的集合(这是必需的,无法通过Web界面从Azure门户通过Azure门户进行此操作),然后从in绑定中删除PartitionKey =“ / id”。

现在一切正常:-)

也许我在使用PartitionKey时出错?...