Azure function with service bus trigger 2.x

2020-09-26T09:44:51.32+00:00

Hi
I have written an Azure function (2.0).
I am trying to consume a queue message (brokered message) like below.

FunctionName("ConsumeQueueMessage")]
        public static async Task InitiateQueueHandling([ServiceBusTrigger("queueName", Connection = "AzureServiceBus")]BrokeredMessage queueMessage, ILogger log)
        {
            log.LogInformation($"HTTP trigger function starting processing campaign seek request at : {DateTime.Now}.");

But when i run it locally I am getting an error like this

Microsoft.Azure.WebJobs.Host: Exception binding parameter 'queueMessage'.
Microsoft.Azure.WebJobs.Host: Binding parameters to complex objects (such as 'BrokeredMessage') uses Json.NET serialization.

  1. Bind the parameter type as 'string' instead of 'BrokeredMessage' to get the raw values and avoid JSON deserialization, or
  2. Change the queue payload to be valid json. The JSON parser failed: Unexpected character encountered while parsing value: @. Path '', line 0, position 0.

I am creating queue message like this

   var message = new BrokeredMessage(objWrapper);
                message.Properties["message"] = JsonConvert.SerializeObject(objWrapper.Message);
                message.Properties["messageType"] = objWrapper.Type.ToString();

Seems to be a nuget package used incorrectly.

The project is developed in .NETCore 3.1

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
583 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,605 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Mike Urnun 9,786 Reputation points Microsoft Employee
    2020-09-29T05:55:40.38+00:00

    Hello @Arjunmenon Unniparambathkalapurayil

    If you're using the version 2x of the Functions runtime, the documentation does state the following in the Usage section:

    These parameter types are for Azure Functions version 1.x; for 2.x and higher, use Message instead of BrokeredMessage.

    As such, you'll want to use the Message type and deserialize the message from byte stream to an appropriate POCO class/object in your project, like below:

        public static class Receiver  
        {  
            [FunctionName("Receiver")]  
            public static void Run([ServiceBusTrigger("myqueue", Connection = "SBConnection")] Message myQueueItem, ILogger log)  
            {  
                log.LogInformation($"C# ServiceBus queue trigger function processed message");  
      
                Person person = JsonConvert.DeserializeObject<Person>(Encoding.UTF8.GetString(myQueueItem.Body));  
                  
                log.LogInformation($"Name: {person.name} \n Age:{person.age}");  
            }  
        }  
      
        public class Person  
        {  
            public string name { get; set; }  
            public int age { get; set; }  
        }  
    

    With the example above, I can send a JSON payload of:

    {  
      "name": "John Doe",  
      "age": "55"  
    }  
    

    and get a response in the log window:

    29000-image.png

    The main reason behind the byte streams is to allow interoperability between different messaging protocols. For more info on this topic, please review: Payload serialization

    Hope this helps - please let me know if you have any further questions.

    0 comments No comments