简体   繁体   中英

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

Using azure functions version 1 it was possible to accept a message as BrokeredMessage.

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

And then retrieve the properties using code similar to this:

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

Using version 2.0 of the function SDK I can't cast the incoming object to a BrokeredMessage without getting a deserialization error message

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.

Is it possible to get the message properties using functions 2.0

Version 2.0 of runtime switched to the new Service Bus client library based on .NET Standard .

BrokeredMessage class is not part of that library, instead it has Message class with comparable functionality but different API.

You should be able to bind your input parameter to this class and then access custom properties via Message.UserProperties dictionary.

In the new world (azure functions .net5) you can no longer use brokered messages. The new libraries do not cater for it.

Function app declaration is no longer [FunctionName=] but [Function= You can no longer receive Message or byte, but only a string.

Example:

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

the magic is now in FunctionContext executionContext you can get properties from this eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM