2
votes

I would like to be able to add custom properties to a queue/topic message as I place it in a queue from and Azure Function. The custom properties are for filtering the messages into different topics. I must be missing something because this working example doesn't seem to have anywhere to put custom properties.

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, 
                            TraceWriter log,
                            ICollector<Contact> outputSbMsg)

    {
        var contactList = await req.Content.ReadAsAsync<ContactList>();

        foreach(var contact in contactList.Contacts)
        {
            if (contact.ContactId == -1)
            {
                continue;
            }

            contact.State = contactList.State;

            outputSbMsg.Add(contact);
        }
    }

I'm coding the function through the Azure Portal. The contact list comes into the function through in the body of an http request. The functions parses out each contact, adds modifies some properties and submits each contact to the queue topic. Additionally I pull other data from the request headers and the contact list and I would like to use that data in the queue topic to filter the requests into different subscriptions.

Edit:

As per @Sean Feldman's suggestion below, the data is added to a BrokeredMessage before adding the BrokeredMessage to the output collection. The key part is to serialize the contact object before adding it to the BrokeredMessage.

 public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, 
                                TraceWriter log,
                                ICollector<BrokeredMessage> outputSbMsg)

        {
            var contactList = await req.Content.ReadAsAsync<ContactList>();

            foreach(var contact in contactList.Contacts)
            {
                if (contact.ContactId == -1)
                {
                    continue;
                }

                string jsonData = JsonConvert.SerializeObject(contact);

                BrokeredMessage message = new BrokeredMessage(jsonData);

                message.Properties.Add("State", contactList.State);

                outputSbMsg.Add(message);
            }
        }

Thank you

1

1 Answers

3
votes

To be able to set custom/user properties, the output collector should be of a native Azure Service Bus message type, BrokeredMessage.

In your case, you'll have to change ICollector<Contact> to ICollector<BrokeredMessage>.