使用 Azure 函数服务总线触发器读取 SB 消息



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

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

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

可以使用BrokeredMessage参数在 Azure 函数服务总线触发器中获取消息正文。

这将返回带有BrokeredMessage.GetBody()方法的消息。

在此处获取更多信息。

在 Azure 门户中,在"查看文件"中添加"project.json"。这是包含代理消息对象的库。

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,并在"Function.json"中将name属性更改为消息

最新更新