首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

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

2012-12-17 
WCF消息WriteMessage时提示反序列化错误(在线等)正在做一个WCF自定义消息编码,继承了MessageEncoder,配置

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>


        </s:Subcode>
      </s:Code>
      <s:Reason>
        <s:Text xml:lang="zh-CN">格式化程序尝试对消息反序列化时引发异常: 对操作“GetData”的请求消息正文进行反序列化时出现错误。根级别上的数据无效。 第 1 行,位置为 571。</s:Text>
      </s:Reason>
    </s:Fault>
  </s:Body>
</s:Envelope>}




紧急求助!
[解决办法]
可以强制 序列化试试
[解决办法]
这个错误应该是wcf要把 Message 转换成参数时发生了错误 
建议你比较一下你上述的xml 是否有问题
[解决办法]
既然有人关注,发下答案,主要是对ArraySegment<byte>使用错误,要注意这是一个字节段,请不要将全部字节做处理。

热点排行