2

I have a service bus queue item BrokeredMessage with the following body:

{"MemberId":711918,"CampaignId":"214ade86-9167-40ba-a63a-9eeb57f893fa"}

The method looks like this:

public static async Task Run(BrokeredMessage myQueueItem, CampaignResult inputDocument, TraceWriter log)

I want to see if the function can pull a document from cosmodb/documentdb with the bindings like this:

"bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "myqueue",
      "connection": "myconnectionsb",
      "accessRights": "Manage"
    },
    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "pmg-docdb-rd-test",
      "collectionName": "JobResults",
      "id": "{CampaignId}-{MemberId}",
      "connection": "myconnection",
      "direction": "in"
    }
  ],

I can't seem to pull it all together. Also, I'm not sure what will actually happen if the document doesn't exist, I would expect the inputDocument to be null but I'm not sure, the idea is I'd like to create it if it doesn't exist and make updates if it does. Since the docs say the any changes to the input document will be persisted I think the updates will work....

The error is: No value for named parameter CampaignId

In Summary, can I have the function retrieve a cosmodb document based on the message and if the document is null it gets created otherwise changes are persisted?

Edit: Just realized that the docdb/cosmodb returns a 404 not found if the document doesn't exist and that I don't think there are any input binding for service bus queue messages, yet.....

lucuma
  • 18,247
  • 4
  • 66
  • 91

2 Answers2

2

Yes, you can do that, but you'd need to bind to a POCO in order to bind to its properties.

In your case, this would look similar to:

public class SomeClass
{
    public string MemberId { get; set; }
    public string CampaignId { get; set; }
}

You can then change your function method to take your POCO, instead of a BrokeredMessage as a parameter for your trigger. The properties in your POCO will become named inputs you can bind to.

Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • I essentially have that with the document db so I'll change it to the queue message and see if it works. – lucuma Jun 01 '17 at 17:03
1

Fabio's post helped me out. The brokered message has to be created like this:

byte[] bytes = Encoding.UTF8.GetBytes(cr.ToJson());
                    MemoryStream stream = new MemoryStream(bytes, writable: false);
                    var msg = new BrokeredMessage(stream)
                    {
                        MessageId = "m-" + j + s.MemberID.ToString(),
                        ScheduledEnqueueTimeUtc = schedule,
                        ContentType = "application/json"
                    };

And the PartitionKey must be provided in the function.json (didn't see this documented). The input documented seem to be null if it wasn't found. I'm not sure how that will work to create it but the input bindings seem to be fine.

lucuma
  • 18,247
  • 4
  • 66
  • 91