当前位置: 代码迷 >> Web Service >> WCF消息WriteMessage时提示反序列化异常()
  详细解决方案

WCF消息WriteMessage时提示反序列化异常()

热度:302   发布时间:2016-05-02 02:45:47.0
WCF消息WriteMessage时提示反序列化错误(在线等)
正在做一个WCF自定义消息编码,继承了MessageEncoder,配置文件已经完成
        public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
        {
            byte[] data = this.compressor.Decompress(buffer.Array);
            ArraySegment<byte> bytes = buffer = new ArraySegment<byte>(data);

            byte[] totalBytes = bufferManager.TakeBuffer(bytes.Count);

            Array.Copy(bytes.Array, 0, totalBytes, 0, bytes.Count);
            ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, 0, bytes.Count);

            bufferManager.ReturnBuffer(byteArray.Array);
            Message msg = innerEncoder.ReadMessage(byteArray, bufferManager, contentType);

            return msg;
        }

得到的请求消息为:
{<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://tempuri.org/IService1/GetData</a:Action>
    <a:MessageID>urn:uuid:ed9f87a4-e78d-49e4-979e-67cab9e6b593</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
    <a:To s:mustUnderstand="1">http://localhost:8731/Design_Time_Addresses/Gridsum.Service/Service1/</a:To>
  </s:Header>
  <s:Body>
    <GetData xmlns="http://tempuri.org/">
      <value>999</value>
    </GetData>
  </s:Body>
</s:Envelope>}

当进入public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)时,消息变成了:

{<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action>
    <a:RelatesTo>urn:uuid:dd5e4d13-e9b7-4364-bac7-14e35ea0f57e</a:RelatesTo>
  </s:Header>
  <s:Body>
    <s:Fault>
      <s:Code>
        <s:Value>s:Sender</s:Value>
        <s:Subcode>
          <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</s:Value>
  相关解决方案