Azure Functions 中的服务总线消息属性?

Service Bus Message Properties in Azure Functions?

我看过一些关于这个主题的旧帖子,但没有任何最新的和与更新的服务总线和 Azure Functions 堆栈相关的内容,所以我想我会再问这个问题。

在使用服务总线触发器的 Azure 函数上,有没有办法访问服务总线消息属性?据我所知,似乎只有消息正文字符串被传递到 运行 方法中。

同样,在使用服务总线输出绑定的 Azure Functions 上,有没有办法将消息属性添加到出站服务总线消息中?同样,据我所知,输出绑定似乎只接受消息正文字符串。

是的,你可以。如何依赖您正在使用的绑定库及其依赖的 SDK 的详细信息。 Microsoft 文档从一开始就不错。消息属性和用户属性都可用。有关详细信息,请参阅 https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger

请注意,使用最新的服务总线 SDK Azure.Messaging.ServiceBus 和新函数 Isolated Worker SDK 对属性的访问有些不同,需要对通过函数执行上下文提供的 JSON 值进行反序列化。

下面是 Azure 函数的示例代码,其中包含用于拦截传入消息的服务总线触发器和用于发送回复消息的服务总线输出绑定。在这两种情况下,服务总线 Message 对象——包含消息属性——被用作消息负载。

请注意,此示例使用(稍旧的)Microsoft.Azure.ServiceBus 库,而不是(最新和最新的)Azure.Messaging.ServiceBus 库。正如@Sean-Feldman 在他的回复中指出的那样,在这个最新的库中处理属性的方式不同。

    public static class WhosebugExample
    {
        // Azure Service Bus constants
        private const string ServiceBusTopic            = "ServiceBusRequest";          // Use your own topic name
        private const string ServiceBusReplyTopic       = "ServiceBusReply";            // Use your own reply topic name
        private const string ServiceBusSubscription     = "AzureFunctionSubscription";  // Use your own subscription name
        private const string ServiceBusConnectionString = "ServiceBusConnectionString"; // As you've defined it in local.settings.json



        [FunctionName("AzureFunctionServiceBus")]  // Use your own Azure Function name
        public static void Run( [ServiceBusTrigger( topicName:        ServiceBusTopic,
                                                    subscriptionName: ServiceBusSubscription,
                                                    Connection = ServiceBusConnectionString )] Message inputMessage, ILogger log,  // Service Bus trigger
                                [ServiceBus( ServiceBusReplyTopic,
                                             EntityType = EntityType.Topic,
                                             Connection = ServiceBusConnectionString )] out Message outputMessage )                // Service Bus output binding
        {
            // Get the message body from the incoming message object.
            string inputMessageBody = Encoding.UTF8.GetString( inputMessage.Body );

            // Get the message properties from the incoming message object.
            // (You can use message properties however you want. In this example,
            // we'll just feed them back into the output message.)
            IDictionary<string, object> messageProperties = inputMessage.UserProperties;



            // Deserialize the incoming message body.
            // (Assumes your own class object is serialized as the incoming message payload.)
            InputClass inputMessageObject = JsonConvert.DeserializeObject<InputClass>( inputMessageBody );



            // Do some work here...



            // Create an output message object.
            // (Assumes your own class object is the basis of the output message payload.)
            OutputClass replyMessageObject = new OutputClass( requestMessage:             inputMessageObject,
                                                              requestReceivedDateTimeUtc: DateTime.UtcNow,
                                                              serviceBusReplyTopic:       ServiceBusReplyTopic,
                                                              status:                     "Success",
                                                              statusMessage:              string.Empty );
            


            // Serialize the output message object
            string replyMessageBody = JsonConvert.SerializeObject( replyMessageObject );



            // Build a Message object for the output message.
            // (The outputMessage variable is defined as an "out" parameter above in the Service Bus output binding.
            // The Service Bus output binding picks up the outputMessage when the Azure Function completes.)
            outputMessage = BuildMessageObject( replyMessageBody, messageProperties );
        }





        // Static method to build and return a Service Bus Message object given a message string
        // and a dictionary of message properties.
        private static Message BuildMessageObject( string message, IDictionary<string, object> messageProperties = null )
        {
            // Create the Service Bus message object
            Message messageObject = new Message( Encoding.UTF8.GetBytes( message ) );


            // Add message properties, if defined
            if (messageProperties != null)
            {
                foreach (var messageProperty in messageProperties)
                {
                    messageObject.UserProperties.Add( messageProperty );
                }
            }


            // Return the message object
            return messageObject;
        }
    }

使用 ServiceBusReceivedMessage 类而不是字符串消息。它有一个 ApplicationProperties 来读取消息属性