Azure Functions和DocumentDB触发器 [英] Azure Functions and DocumentDB triggers

查看:37
本文介绍了Azure Functions和DocumentDB触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在写入DocumentDB时指定DocumentDB将触发触发器?

Is it possible to specify the DocumentDB is to fire triggers when writing to DocumentDB?

我有一个Azure函数,可将JSON消息从Service Bus队列中拉出,并将其放入DocumentDB中,如下所示:

I have an Azure function that pulls JSON messages off a Service Bus Queue and puts them into DocumentDB like so:

using System;
using System.Threading.Tasks;

public static string Run(string myQueueItem, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

    return myQueueItem;
}

在将新文档添加到服务总线队列时,这会将新文档插入数据库中,但是在添加新文档并添加附件时,我需要DocumentDB进行处理.在当前设置中无法完成此操作,我想告诉DocumentDB触发一个触发器.

This inserts new documents into the database as they are added to the service bus queue, however I need DocumentDB to process these as they are added and add attachments. This cannot be done in the present setup and I would like to tell DocumentDB to fire a trigger.

我尝试过这样的事情:

using System;
using System.Threading.Tasks;

public static string Run(string myQueueItem, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");

    return "x-ms-documentdb-post-trigger-include: addDocument\n" + myQueueItem;
}

它不起作用,并给我这样的错误:

It doesn't work and gives me errors like this:

执行函数时发生异常: Functions.ServiceBusQueueTriggerCSharp1. Microsoft.Azure.WebJobs.Host: 函数返回后处理参数_return时出错: Newtonsoft.Json:解析值时遇到意外字符: X.路径",第0行,位置0.

Exception while executing function: Functions.ServiceBusQueueTriggerCSharp1. Microsoft.Azure.WebJobs.Host: Error while handling parameter _return after function returned:. Newtonsoft.Json: Unexpected character encountered while parsing value: x. Path '', line 0, position 0.

我喜欢这种设置,因为我可以在队列中添加添加记录的请求,并且它们只是缓冲直到数据库可以处理它为止(这处理了需求激增),但是它允许从客户端计算机上卸载数据的速度与之相同.网络可以承载它,然后当需求再次下降时,队列/数据库组合就会陷入困境.

I like this setup because I can saturate the queue with requests to add records and they just buffer until the database can deal with it, which deals with spikes in demand, but it allows data offload from the client machine as fast as the network can carry it and then the queue/database combination gets caught up when demand drops again.

推荐答案

您可以参考下面的代码示例,以在Azure Functions中启用触发器的情况下创建文档.

You could refer to the following code sample to create document with the trigger enabled in Azure Functions.

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

public static void Run(string myQueueItem, TraceWriter log)
{
    string EndpointUri = "https://{documentdb account name}.documents.azure.com:443/";
    string PrimaryKey = "{PrimaryKey}";

    DocumentClient client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);

    client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("{databaseid}", "{collenctionid}"), new MyChunk { MyProperty = "hello" },
               new RequestOptions
               {
                   PreTriggerInclude = new List<string> { "YourTriggerName" },
               }).Wait();

    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}

public class MyChunk
{
    public string MyProperty { get; set; }
}

注意 :要在C#函数中使用Microsoft.Azure.DocumentDB NuGet包,请 project.json

 {
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.Azure.DocumentDB": "1.13.1"
      }
    }
   }
}

此外,请确保已在DocumentDB中创建了触发器,有关创建触发器的详细信息,请参考

Besides, please make sure you have created triggers in your DocumentDB, for details about creating triggers, please refer to this article.

这篇关于Azure Functions和DocumentDB触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆