【问题标题】:read SB message with azure functions service bus trigger使用 azure 功能服务总线触发器读取 SB 消息
【发布时间】:2018-09-27 06:34:51
【问题描述】:

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

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

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

【问题讨论】:

  • 您想将 mySbMsg 读取为 Json 格式吗?还是要分析json消息?
  • 这里是关于从Azure服务总线主题订阅接收JSON对象的问题,您可以参考it

标签: azure azure-functions azureservicebus


【解决方案1】:

可以使用BrokeredMessage参数在Azure Function Service Bus Trigger中获取消息体。

这将使用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" 中的消息

【讨论】:

  • 我得到找不到类型或命名空间名称“BrokeredMessage”(您是否缺少 using 指令或程序集引用?)。我尝试使用 Microsoft.ServiceBus;但它仍然失败
  • 对于 2.x 版本的功能,您应该使用 Message 而不是 BrokeredMessage
  • 你能给我发一个示例代码吗..默认是 [FunctionName("Function1")] public static void Run([ServiceBusTrigger("mytopic", "mysubscription", Connection = " ")]string mySbMsg, ILogger log) { log.LogInformation($"C# ServiceBus 主题触发函数处理的消息:{mySbMsg}"); } }
猜你喜欢
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 2022-07-03
  • 2021-09-09
  • 1970-01-01
相关资源
最近更新 更多