首页 > 解决方案 > 具有 CosmosDB 输入的时间触发功能

问题描述

我在 azure 函数中创建了一个时间触发函数,并添加了一个 CosmosDB 输入,如下所示。

在此处输入图像描述

下面是 .csx 文件

#r "Microsoft.Azure.Documents.Client"

using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

public static async Task Run(TimerInfo myTimer, string[] inputDocument, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

    // string [] = bindings.inputDocument;

    DocumentClient client;
}

如何从 cosmosDb 获取输入文档到这个 csx 文件中?

我对 C# 不熟悉,在 javascript 中我们将使用var Data = context.bindings.DataInput; How to do the same in c#?

标签: azureazure-functionsazure-cosmosdbazure-cosmosdb-sqlapi

解决方案


您可以像下面的代码片段一样使用它

public static void Run(TimerInfo myTimer, IEnumerable<dynamic> documents)
    {
        foreach (var doc in documents)
        {
            // operate on each document
        }
    }

文档中的更多示例

评论中的问题

如果我们有多个 cosmos Db 输入,是否需要添加如下?

即使您有多个输入,IEnumerable<dynamic> documents也不会使用。你可以迭代列表。

如果我们有 cosmosDB 输出,如何添加?

在 this 中使用的out object是指向您的绑定。

public static void Run(string myQueueItem, out object employeeDocument, ILogger log)
    {
      log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

      dynamic employee = JObject.Parse(myQueueItem);

      employeeDocument = new {
        id = employee.name + "-" + employee.employeeId,
        name = employee.name,
        employeeId = employee.employeeId,
        address = employee.address
      };
    }

有关输出的更多信息


推荐阅读