Azure Functions and DocumentDB triggers

安稳与你 提交于 2019-12-12 18:17:56

问题


Is it possible to specify the DocumentDB is to fire triggers when writing to 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;
}

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.

I have tried something like this:

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:

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.


回答1:


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

Note: for using Microsoft.Azure.DocumentDB NuGet package in a C# function, please upload a project.json file to the function's folder in the function app's file system.

project.json

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

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



来源:https://stackoverflow.com/questions/43646389/azure-functions-and-documentdb-triggers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!