Fac*_*izr 5 servicebus azureservicebus azure-functions
我看过一些有关此主题的旧帖子,但没有任何最新内容且与较新的服务总线和 Azure Functions 堆栈相关,因此我想再次问这个问题。
在使用服务总线触发器的 Azure 函数上,有没有办法访问服务总线消息属性?据我所知,似乎只有消息正文字符串被传递到 Run 方法中。
同样,在使用服务总线输出绑定的 Azure 函数上,是否有办法将消息属性添加到出站服务总线消息?同样,据我所知,输出绑定似乎只接受消息正文字符串。
以下是 Azure Function 的示例代码,其中包含用于拦截传入消息的服务总线触发器和用于发送回复消息的服务总线输出绑定。在这两种情况下,服务总线Message
对象(包含消息属性)都用作消息负载。
请注意,此示例使用(稍旧的)Microsoft.Azure.ServiceBus
库,而不是(最新的和最新的)Azure.Messaging.ServiceBus
库。正如 @Sean-Feldman 在他的回复中指出的,在这个最新的库中,属性的处理方式有所不同。
public static class StackOverflowExample
{
// 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: tring.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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8482 次 |
最近记录: |