0

I'm passing messages to my Azure service bus queue like this, where the 'MessageId' value is a string of a number ex. '345'. I see it in the queue in my Azure portal dashboard and when I look at the message in Service Bus Explorer I see the message id as the eventId I'm passing it.

 Message message = new Message
 {
     MessageId = eventId.ToString(), // passing it a value of '123'
     ScheduledEnqueueTimeUtc = enqueuedTime
 };

 var sendCodeSequence = await queueClient.ScheduleMessageAsync(message, new DateTimeOffset(enqueuedTime));

But when my function gets triggered I'm getting an exception saying the myQueueItem from the function is either 0 or null, depending on which function I'm calling.

Here is the first couple of lines of code for one of the functions that are giving me this problem. The event is always null and throws an exception because myQueueItem is always null or 0.

I had this working before, I thought, but now I think changes were made and throwing exceptions each time my functions get triggered!

Question - Is myQueueItem not the messageId I'm passing it when I create and put the message on the queue?

[FunctionName("CancelEvent")]
    public static void Run([ServiceBusTrigger("canceleventqueue", AccessRights.Manage, Connection = "events2017_RootManageSharedAccessKey_SERVICEBUS")]string myQueueItem, TraceWriter log, ExecutionContext context)
    {
        try
        {
            log.Info($"Cancel event started id: {myQueueItem}");

            var eventId = Convert.ToInt32(myQueueItem);

            using (var dbContext = new EventContext(myDatabaseConnectionString))
            {
                var event = dbContext.Events.Where(i => i.EventId == eventId).FirstOrDefault();

                if (Event == null)
                    throw new Exception("no event with this id: " + myQueueItem);
Jayendran
  • 9,638
  • 8
  • 60
  • 103
chuckd
  • 13,460
  • 29
  • 152
  • 331

2 Answers2

0

myQueueItem is the message itself. string myQueueItem isn't really useful not if you have used a brokered message or service bus message and added other information you want to use in the function. You should replace string myQueueItem with Microsoft.Azure.ServiceBus.Message myQueueItem. Also review the implementation in this post for some other useful information on serializing/deserializing the message if neccessary: Azure ServiceBus Message Serialization/Deserialization

Mehdi Ibrahim
  • 2,434
  • 1
  • 13
  • 13
  • I only need to pass an id from the message to the function, how do you recommend I do this? Additionaly, what I don't understand is that I had this working before and passing an id as the 'myQueueItem', now it's not working! – chuckd Aug 26 '18 at 04:39
  • Have you tried var sendCodeSequence = await queueClient.ScheduleMessageAsync(eventId.ToString(), new DateTimeOffset(enqueuedTime)); ? – Mehdi Ibrahim Aug 26 '18 at 04:53
  • no, the first parameter only takes a message object – chuckd Aug 26 '18 at 04:56
  • Now I'm just wondering if it broke because I think I added a scheduled enqueue time to the message. maybe now that changed it's structure so I can read it as a string, where as before I just had the id as a string and it allowed me to read it as a string without casting it to a object or reading it in as a Message object first.. – chuckd Aug 26 '18 at 05:00
  • That probably explains it. If you want to continue to use your existing structure, then you should replace string myQueueItem with BrokeredMessage myQueueItem. Otherwise, if you just want to send a single string, you can either revert back your code or use QueueClient.SendAsync(). Please also review the serialization scenarios for ServiceBusTrigger here: https://github.com/Azure/azure-webjobs-sdk/wiki/ServiceBus-Serialization-Scenarios – Mehdi Ibrahim Aug 26 '18 at 05:07
  • If I want to continue using my existing structure, shouldn't pass in a 'Microsoft.Azure.ServiceBus.Message' like in your original response. That's the same type of object I put on the queue! I'm a little confused now. – chuckd Aug 26 '18 at 05:09
  • The best way is to try both and see which works. Sorry, I don't have an executable version of your code otherwise I would've been a little more precise. The ServiceBusTrigger documentation indicates compatibility with BrokeredMessage. Microsoft.Azure.ServiceBus.Message is a newer construct and ServiceBusTrigger may not yet support it. If it will only work with a BrokeredMessage you will need to send a BrokeredMessage instead of a Microsoft.Azure.ServiceBus.Message. – Mehdi Ibrahim Aug 26 '18 at 05:17
  • Do you have any link to the most recent documentation for the functions? I can never find anything and when I do it's usually out of date or not what I need. – chuckd Aug 26 '18 at 05:18
  • Either way thanks for the help that helps me out a lot! thanks! – chuckd Aug 26 '18 at 05:19
  • https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus . I've also posted a few links in my previous messages. Hope this helps, good luck! – Mehdi Ibrahim Aug 26 '18 at 05:22
  • Can you describe your final solution so I can update my answer? – Mehdi Ibrahim Aug 26 '18 at 13:48
0

I solved it by passing in 'string messageId' as a parameter in the function. I guess Azure messages are used differently then BrokeredMessages.

chuckd
  • 13,460
  • 29
  • 152
  • 331