如何访问 Azure Function 2 中的 Azure 服务总线消息属性

How to access azure service bus message properties in Azure function 2

使用 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

使用函数 SDK 的 2.0 版我无法在没有收到反序列化错误消息的情况下将传入对象转换为 BrokeredMessage

There was an error deserializing the object of type Microsoft.ServiceBus.Messaging.BrokeredMessage. The input source is not correctly formatted. System.Private.DataContractSerialization: The input source is not correctly formatted.

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

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

BrokeredMessage class 不是该库的一部分,相反它具有 Message class 具有可比较的功能但不同 API.

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

在新世界 (azure functions .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();