如何使用Azure Logic Apps发送的服务总线消息 [英] How to consume service bus message sent via Azure Logic Apps

查看:105
本文介绍了如何使用Azure Logic Apps发送的服务总线消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure Logic应用程序正在取代Azure Scheduler(很快就会弃用)。


Azure Logic App充当调度程序,它是重复出现的应用程序,每5分钟应该发送一个空的json有效负载({})到服务总线队列。


我正在尝试将此有效负载作为字符串发送,因为有时候,我可能触发特定的操作指定日期。在这种情况下,发布到服务总线队列的逻辑应用程序有效负载将更加复杂:{" Date" :"01-31-2019"}。


消息正在发布到Azure Service Bus,但是,在消费端,我无法使用它。

 public void MessageHandler(BrokeredMessage triggerMessage)
{
//这会失败
triggerMessage.GetBody< string>();

//这失败
triggerMessage.GetBody< byte []>();

//这会失败
triggerMessage.GetBody< MyDto>();
}

异常消息说明:


反序列化时出错System.String类型的对象。输入源格式不正确。


堆栈跟踪:


    at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader,Boolean verifyObjectName,DataContractResolver dataContractResolver)

    at Microsoft.ServiceBus.Messaging.DataContractBinarySerializer.ReadObject(XmlDictionaryReader reader,Boolean verifyObjectName)

    at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader,Boolean verifyObjectName,DataContractResolver dataContractResolver)

    at Microsoft.ServiceBus.Messaging.DataContractBinarySerializer.ReadObject(Stream stream)

    at Microsoft.ServiceBus.Messaging.BrokeredMessage.GetBody [T](XmlObjectSerializer serializer)


这是Content& amp;屏幕截图来自Logic App设计器的Azure Service Bus消息的内容类型。



所以问题是,如何序列化从Logic App传递的Service Bus消息?


我必须使用 BrokeredMessage ,转移到新SDK并不是一个真正的选择。


我正在使用 WindowsAzure.ServiceBus 版本 4.1.11


解决方案

< blockquote>

事实证明,在这种情况下,消息内容应该作为Stream读取并转换为字符串

 Stream stream = message.GetBody< Stream>() ; 
using(TextReader reader = new StreamReader(stream))
{
string messageContents = await reader.ReadToEndAsync();
}

令人惊讶的是,这个相同的代码不适用于Azure Scheduler。


不确定Azure App服务发布2个不同有效负载的原因是什么。



I have an Azure Logic App which is replacing Azure Scheduler (which soon to be deprecated).

Azure Logic App acts as a scheduler, it is recurring App, which every 5 minutes should send an empty json payload ({}) to service bus queue.

I'm trying to send this payload as a string, because sometimes, to trigger specific action I might specify Dates. In which case Logic App payload published to Service Bus queue will be more involved: {"Date" : "01-31-2019"}.

Message is being published to Azure Service Bus, however, on consuming end I fail to consume it.

public void MessageHandler(BrokeredMessage triggerMessage)
{
   // this fails
   triggerMessage.GetBody<string>();

   // this fails
   triggerMessage.GetBody<byte[]>();

   // this fails
   triggerMessage.GetBody<MyDto>();
}

Exception messages says:

There was an error deserializing the object of type System.String. The input source is not correctly formatted.

Stack Trace:

   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at Microsoft.ServiceBus.Messaging.DataContractBinarySerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at Microsoft.ServiceBus.Messaging.DataContractBinarySerializer.ReadObject(Stream stream)
   at Microsoft.ServiceBus.Messaging.BrokeredMessage.GetBody[T](XmlObjectSerializer serializer)

Here's the screenshot of Content & Content Type of Azure Service Bus message from Logic App designer.

So question is, how to serialize Service Bus message passed from Logic App ?

I have to use BrokeredMessage, moving to new SDK is not really an option.

I'm using WindowsAzure.ServiceBus version 4.1.11

解决方案

It turns out in this case message content should be read as a Stream and converted to string

Stream stream = message.GetBody<Stream>();
using (TextReader reader = new StreamReader(stream))
{
    string messageContents = await reader.ReadToEndAsync();
}

Surprisingly, this same code doesn't work with Azure Scheduler.

Not sure what's the reason of having 2 different payloads published by Azure App services.


这篇关于如何使用Azure Logic Apps发送的服务总线消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆