使用天蓝色功能服务总线触发器读取SB消息

时间:2018-09-27 06:34:51

标签: azure azure-functions azureservicebus

我刚刚使用服务总线触发器创建了一个简单的azure函数。我正在使用提供的默认示例。我可以在下面的代码中阅读messageid

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

我正在努力寻找代码,以显示如何阅读发布的json消息。 谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用BrokeredMessage参数在Azure Function Service Bus触发器中获取消息正文。

这将使用BrokeredMessage.GetBody()方法返回消息。

获取更多信息here

在Azure门户中,在“查看文件”中添加“ project.json”。该库包含BrokeredMessage对象。

project.json应该看起来像

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

保存时,您可以看到软件包已恢复。

在Run方法内,添加BrokeredMessage作为参数。该方法应该看起来像

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);
}

别忘了在“ Run.csx”中添加Using Microsoft.ServiceBus.Messaging并将name属性更改为“ Function.json”中的消息