2

I want to create an Azure function that is triggered by an Event Hub message. I also want to use a Document from DocumentDb, taking the DocumentId from the content of the trigger message (the event hub message). I don't see how this is possible and I doubt if it is, but I wanted to give this a try. In the input, I selected the DocumentDB and in the DocumentId input box (defaulted to {documentId}, I entered {myEventHubMessage.DocumentId}, where myEventHubMessage is the name of my trigger and DocumentId the json property in the content of the message.

Any idea if this is possible and how I could workaround this (without hardcoding DocDb connection strings in my function)

1 Answer 1

5

Yes this is possible. Below is a C# example, showing first the code, then the binding metadata. For other languages like Node, the binding metadata would be the same, just the code differs. The DocumentDB binding binds to the DocId property of the incoming message via binding expression {DocId}.

Here's the code:

#r "Microsoft.ServiceBus"

using System;
using Microsoft.ServiceBus.Messaging;

public static void Run(MyEvent evt, MyDocument document, TraceWriter log)
{
    log.Info($"C# Event Hub trigger function processed event: {evt.Id}");
    log.Info($"Document {document.Id} loaded. Value {document.Value}");
}

public class MyEvent
{
    public string Id { get; set; }
    public string DocId { get; set; }
}

public class MyDocument
{
    public string Id { get; set; }
    public string Value { get; set; }
}

And the binding metadata:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "evt",
      "direction": "in",
      "path": "testhub",
      "connection": "<your connection>"
    },
    {
      "type": "documentdb",
      "name": "document",
      "databaseName": "<your database>",
      "collectionName": "<your collection>",
      "id": "{DocId}",
      "connection": "<your connection>",
      "direction": "in"
    }
  ]
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.