Error while reading body of request message

自作多情 提交于 2021-01-27 11:31:37

问题


I need to read content of message in WCF project like

 var messageContent = Encoding.UTF8.GetString(OperationContext.Current.RequestContext.RequestMessage.GetBody<byte[]>());

But in result I got an error:

Expecting element 'base64Binary' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'Human', namespace 'http://numans.hr-xml.org/2007-04-15'.

Can you please suggest me what Im doing wrong?

Content that I'm sending are:

<Human xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://numans.hr-xml.org/2007-04-15">
  <HumanId>
    <guid>c2ee9a7e-c7a8-48e0-910b-47c2012bfa8e</guid>
  </HumanId>
  ...
</Human>

Also I tried to read content like:

var messageContent = OperationContext.Current.RequestContext.RequestMessage.ToString();

Result of messageContent:

...stream...


回答1:


GetBody<T> is used to deserialize the message body as type T. So when you call GetBody<byte[]>(), the deserializer expects base64-encoded binary data but finds the <Human> element.

If you only want to read the message body as string, use GetReaderAtBodyContents which returns an XmlDictionaryReader, at which you can use ReadOuterXml().

If you want to read the body as typed content, create a Human class from its XML representation and use GetBody<Human>().



来源:https://stackoverflow.com/questions/33998431/error-while-reading-body-of-request-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!