我需要在WCF项目中阅读消息内容
var messageContent = Encoding.UTF8.GetString(OperationContext.Current.RequestContext.RequestMessage.GetBody<byte[]>());
Run Code Online (Sandbox Code Playgroud)
但结果我得到一个错误:
期望命名空间' http://schemas.microsoft.com/2003/10/Serialization/ '中的元素'base64Binary' .遇到名为'Human'的'Element',命名空间' http://numans.hr-xml.org/2007-04-15 '.
你能告诉我我做错了什么吗?
我发送的内容是:
<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>
Run Code Online (Sandbox Code Playgroud)
我还尝试阅读以下内容:
var messageContent = OperationContext.Current.RequestContext.RequestMessage.ToString();
Run Code Online (Sandbox Code Playgroud)
messageContent的结果:
...流...
GetBody<T>
用于将消息体反序列化为类型T.因此,当您调用时GetBody<byte[]>()
,反序列化器需要base64编码的二进制数据,但会找到该<Human>
元素.
如果您只想将消息体读取为string
,则使用GetReaderAtBodyContents
返回XmlDictionaryReader
可以使用的消息体ReadOuterXml()
.
如果要将主体作为类型化内容读取,请Human
从其XML表示形式创建一个类并使用GetBody<Human>()
.