【问题标题】:ServiceBus message handler using Windows.Azure.ServiceBus (5.2.0) fails to deserialise body stream using DataContractSerializer NET 4.6.1使用 Windows.Azure.ServiceBus (5.2.0) 的 ServiceBus 消息处理程序无法使用 DataContractSerializer NET 4.6.1 反序列化正文流
【发布时间】:2021-12-28 09:57:20
【问题描述】:

我正在使用 Windows.Azure.ServiceBus (5.2.0) 来接收和处理 Azure ServiceBus 消息。作为其中的一部分,我需要将消息正文作为字符串访问以创建校验和。代码如下:

if (messageBody.StartsWith("@"))
{
    DataContractSerializer serializer = new(typeof(string));
    using XmlDictionaryReader xmlDictionaryReader = 
    XmlDictionaryReader.CreateBinaryReader(brokeredMessage.GetBody<Stream>(), XmlDictionaryReaderQuotas.Max);
    messageBody = (string)serializer.ReadObject(xmlDictionaryReader);
}

如果我创建一个 StreamReader 并以 UTF8 格式从流中读取文本,则输出为 @string3http://schemas.microsoft.com/2003/10/Serialization/��{"Prop1":null,"Prop2":null,"Prop3":null,"Prop4":null,"Prop5":null}

(string)serializer.ReadObject(xmlDictionaryReader) 处的错误是“期望来自命名空间 'http://schemas.microsoft.com/2003/10/Serialization/' 的元素 'string'.. 遇到名称为 ''、命名空间 '' 的 'None' 。”

我想要的是从有效负载中获取 JSON 作为字符串。 BrokeredMessage 是使用 JSON 字符串作为正文创建的,然后再发布到由上述代码订阅和处理的主题。

【问题讨论】:

  • 试试 XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(message.Body.ToStream(), XmlDictionaryReaderQuotas.Max);

标签: c# azure azureservicebus


【解决方案1】:

正如HarshithaVeeramalla-MT 建议的那样,您能否尝试使用以下代码代替您的代码。

XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(message.Body.ToStream(), XmlDictionaryReaderQuotas.Max);

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 2021-02-13
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多