String 使用azure逻辑应用程序将消息作为字符串发送到azure服务总线

String 使用azure逻辑应用程序将消息作为字符串发送到azure服务总线,string,stream,azureservicebus,azure-logic-apps,azure-servicebus-topics,String,Stream,Azureservicebus,Azure Logic Apps,Azure Servicebus Topics,我正在使用逻辑应用程序操作“发送消息”向服务总线主题发送消息。在控制台应用程序中读取时,如果执行以下操作: SubscriptionClient subClient = SubscriptionClient.CreateFromConnectionString(connstr, topicName, subscriptionName); OnMessageOptions options = new OnMessageOptions(); options.AutoComplete = true;

我正在使用逻辑应用程序操作“发送消息”向服务总线主题发送消息。在控制台应用程序中读取时,如果执行以下操作:

SubscriptionClient subClient = SubscriptionClient.CreateFromConnectionString(connstr, topicName, subscriptionName);
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = true; 
options.MaxConcurrentCalls = 1;  
subClient.OnMessage((message) => {
    string sjson = null;
    try
    {
        sjson = message.GetBody<string>();
        Console.WriteLine(sjson);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}, options);
事实证明,Logic应用程序将消息作为流而不是字符串发送,这就是引发异常的原因。因为,如果我这样做,控制台应用程序能够读取消息:

SubscriptionClient subClient = SubscriptionClient.CreateFromConnectionString(connstr, topicName, subscriptionName);
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = true; 
options.MaxConcurrentCalls = 1;  
subClient.OnMessage((message) => {
    Stream stream;
    StreamReader reader;
    string messageJson;
    try
    {
        stream = message.GetBody<Stream>();
        reader = new StreamReader(stream);
        messageJson = reader.ReadToEnd();
        Console.WriteLine(messageJson);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}, options);
SubscriptionClient subClient=SubscriptionClient.CreateFromConnectionString(connstr,topicName,subscriptionName);
OnMessageOptions=新建OnMessageOptions();
options.AutoComplete=true;
options.MaxConcurrentCalls=1;
子客户端OnMessage((消息)=>{
溪流;
流阅读器;
字符串messageJson;
尝试
{
stream=message.GetBody();
读卡器=新的流读卡器(流);
messageJson=reader.ReadToEnd();
Console.WriteLine(messageJson);
}
捕获(例外情况除外)
{
Console.WriteLine(例如ToString());
}
},选项);
所以,我的问题是,有没有办法让logic应用程序以字符串而不是流的形式发送消息?或者这是逻辑应用程序的一个限制


我已经尝试将“application/json”、“System.String”和“text/plain”作为Logic App action中消息的内容类型,但它不起作用。

Logic apps服务总线连接器当前仅将数据作为字节流转储到服务总线。根据Azure Logic应用程序的当前设计,无法以字符串形式发送消息

不幸的是,无法更改正文的传递方式。为了解决这个问题,我们用一个helper方法更新了侦听器,该方法试图找出主体的类型,并相应地提取:

private static JObject GetBody(BrokeredMessage brokeredMessage)
        {
            string objAsJson;
            object objIsStream;

            brokeredMessage.Properties.TryGetValue("isStream", out objIsStream);
            bool bIsStream = objIsStream != null ? (bool)objIsStream : brokeredMessage.DeliveryCount % 2 == 0; // Default delivery method is String; Retry String as Stream if it fails

            if (bIsStream)
            {
                // Azure Functions and Logic Apps send messages as Stream
                // Service Bus Explorer defaults to Stream but can send as String
                Stream stream = brokeredMessage.GetBody<Stream>();
                StreamReader reader = new StreamReader(stream);
                objAsJson = reader.ReadToEnd();
            }
            else
            {
                // Our services send messages as String
                objAsJson = brokeredMessage.GetBody<string>();
            }

            return JObject.Parse(objAsJson);
        }
私有静态JObject GetBody(BrokeredMessage BrokeredMessage)
{
字符串objAsJson;
对象流;
brokeredMessage.Properties.TryGetValue(“isStream”,out-ObjiStream);
bool bistream=objistream!=null?(bool)objistream:brokeredMessage.DeliveryCount%2==0;//默认传递方法为String;如果失败,则作为流重试String
if(双流)
{
//Azure函数和逻辑应用程序以流形式发送消息
//服务总线资源管理器默认为流,但可以作为字符串发送
Stream=brokeredMessage.GetBody();
StreamReader=新的StreamReader(流);
objAsJson=reader.ReadToEnd();
}
其他的
{
//我们的服务以字符串形式发送消息
objAsJson=brokeredMessage.GetBody();
}
返回JObject.Parse(objAsJson);
}

objistream是否始终为空?当my Azure函数向服务总线发送消息时,它会添加
isStream
属性并将其设置为
true
,以明确告知侦听器该类型为Stream。
private static JObject GetBody(BrokeredMessage brokeredMessage)
        {
            string objAsJson;
            object objIsStream;

            brokeredMessage.Properties.TryGetValue("isStream", out objIsStream);
            bool bIsStream = objIsStream != null ? (bool)objIsStream : brokeredMessage.DeliveryCount % 2 == 0; // Default delivery method is String; Retry String as Stream if it fails

            if (bIsStream)
            {
                // Azure Functions and Logic Apps send messages as Stream
                // Service Bus Explorer defaults to Stream but can send as String
                Stream stream = brokeredMessage.GetBody<Stream>();
                StreamReader reader = new StreamReader(stream);
                objAsJson = reader.ReadToEnd();
            }
            else
            {
                // Our services send messages as String
                objAsJson = brokeredMessage.GetBody<string>();
            }

            return JObject.Parse(objAsJson);
        }