0

I just created a simple azure function using service bus trigger. I am using the default example provided. I can read the messageid in the code below

public static void Run(string mySbMsg, TraceWriter log)
{
log.Info($"C# ServiceBus topic trigger function processed message: 
{mySbMsg}");
}

I`m struggling to find codes showing how to read the json message that was posted. Thanks for you help

CHEEKATLAPRADEEP
  • 12,191
  • 1
  • 19
  • 42
user3913926
  • 47
  • 1
  • 4
  • Do you want to read the mySbMsg as Json format? Or you want to analyze the json message? – Joey Cai Sep 27 '18 at 09:56
  • Here is an issue about receive JSON object from the azure service bus topic subscriptions, you could refer to [it](https://stackoverflow.com/questions/42006478/how-to-receive-json-object-from-the-azure-service-bus-topic-subscriptions). – Joey Cai Sep 27 '18 at 10:04

1 Answers1

0

You can use the BrokeredMessage parameter to get the message body in Azure Function Service Bus Trigger.

This will return the message with the BrokeredMessage.GetBody() method.

Get more information here.

In Azure portal, add "project.json" in View Files. This the library which contains BrokeredMessage object.

The project.json should look like

{
  "frameworks": 
    {  
      "net46":
      { 
        "dependencies":
        {
          "WindowsAzure.ServiceBus": "4.1.2"
        }
      }
    }
}

When you save, you can see the package gets restored.

Inside the Run method, add BrokeredMessage as parameter. The method should look like

public static void Run(BrokeredMessage message, TraceWriter log)
{
string messageBody = message.Properties["Message"].ToString();
string messageId = message.Properties["Id"].ToString();
log.Info($"message - " + messageBody + " Id " + messageId);
}

Don't forget to add Using Microsoft.ServiceBus.Messaging in "Run.csx" and change the name property to message in "Function.json"

Arunprabhu
  • 1,454
  • 9
  • 15
  • I get The type or namespace name 'BrokeredMessage' could not be found (are you missing a using directive or an assembly reference?). I tried using Microsoft.ServiceBus; but it still fails – user3913926 Sep 27 '18 at 11:46
  • For functions of version 2.x you should use Message instead of BrokeredMessage – Arunprabhu Sep 27 '18 at 11:51
  • Can you please post a sample code for me ..the default one is [FunctionName("Function1")] public static void Run([ServiceBusTrigger("mytopic", "mysubscription", Connection = "")]string mySbMsg, ILogger log) { log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}"); } } – user3913926 Sep 28 '18 at 04:58