问题
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