【问题标题】:How to access azure service bus message properties in Azure function 2如何在 Azure 函数 2 中访问 azure 服务总线消息属性
【发布时间】:2018-04-16 15:19:32
【问题描述】:

使用 azure 函数版本 1,可以将消息作为 BrokeredMessage 接受。

public static void Run([ServiceBusTrigger("MySServiceBus", "MySubscriptionName", AccessRights.Listen, Connection = "MyConnectionString")]BrokeredMessage message, TraceWriter log)

然后使用类似这样的代码检索属性:

var MyProperty = message.Properties["MyMessageProperty"] as string

使用 2.0 版的函数 SDK 我无法将传入的对象转换为 BrokeredMessage 而不会收到反序列化错误消息

反序列化类型对象时出错 Microsoft.ServiceBus.Messaging.BrokeredMessage。输入源是 格式不正确。 System.Private.DataContractSerialization: 输入源格式不正确。

是否可以使用函数2.0获​​取消息属性

【问题讨论】:

  • 很抱歉造成混淆 - 我们正在更新关于服务总线绑定的文档以反映这一变化。

标签: c# azure-functions azureservicebus azure-servicebus-topics


【解决方案1】:

2.0 版运行时切换到new Service Bus client library based on .NET Standard

BrokeredMessage 类不是该库的一部分,而是具有类似功能但 API 不同的 Message 类。

您应该能够将输入参数绑定到此类,然后通过Message.UserProperties 字典访问自定义属性。

【讨论】:

  • 谢谢米哈伊尔,这正是我想要的。我认为文档很快就会赶上。公平地说,2.0 仍然是预览版
  • 我们可以在对服务总线进行输出绑定时使用IAsyncCollector<BrokeredMessage> 以便能够向消息添加自定义属性吗?
【解决方案2】:

在新世界(天蓝色函数 .net5)中,您不能再使用代理消息。新的库不适合它。

函数应用声明不再是[FunctionName=],而是[Function= 您不能再接收Message 或字节,而只能接收一个字符串。

例子:

 [Function("TestFA")]
 public async Task Run([ServiceBusTrigger(topicName, subscriberName, Connection = ???)] string messageText, string id,  FunctionContext executionContext)

现在的魔力在 FunctionContext executionContext 您可以从中获取属性 例如

KeyValuePair<string, object> props = executionContext.BindingContext.BindingData.Where(x => x.Key == "UserProperties").FirstOrDefault();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2019-01-23
    • 2020-06-11
    • 1970-01-01
    • 2014-04-09
    相关资源
    最近更新 更多