我想通了。首先,当我说我只使用MTOM编码器获得多部分信息的第一部分时,我是不正确的;我得到了整件事情。我正在调试器中查看它,并且底部必须已经在调试查看器中被截断。根据我的经验,手动查看和解密多部分消息。
第二点,我所要做的就是在Content-Type为multipart/related时使用MTOM编码器,并且一切正常。如果您阅读了上面的参考文章,则只需动态检测消息是多部分还是常规文本,然后根据该文本选择合适的编码器。实质上,它是一种自定义编码器,它内置了文本编码器和MTOM编码器,并根据传入消息的内容类型来回切换。
我们的项目在交给主程序逻辑之前需要对响应消息进行一些后处理。因此,我们将传入的SOAP内容作为XML字符串进行处理,并对其进行一些XML操作。
这与本文推荐的解决方案略有不同。本文解决方案中所需的全部内容是使用正确的编码器将消息读入System.ServiceModel.Channels.Message并返回。在我们的解决方案中,我们需要中断这个过程并进行后期处理。
要做到这一点,实现您的自定义编码器执行以下操作:
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
//First, get the incoming message as a byte array
var messageData = new byte[buffer.Count];
Array.Copy(buffer.Array, buffer.Offset, messageData, 0, messageData.Length);
bufferManager.ReturnBuffer(buffer.Array);
//Now convert it into a string for post-processing. Look at the content-type to determine which encoder to use.
string stringResult;
if (contentType != null && contentType.Contains("multipart/related"))
{
Message unprocessedMessageResult = this.mtomEncoder.ReadMessage(buffer, bufferManager, contentType);
stringResult = unprocessedMessageResult.ToString();
}
else {
//If it's not a multi-part message, the byte array already has the complete content, and it simply needs to be converted to a string
stringResult = Encoding.UTF8.GetString(messageData);
}
Message processedMessageResult = functionToDoPostProccessing(stringResult);
return processedMessageResult;
}
不要忘了,这是确定以纪念自己的答案是正确的 - http://meta.stackexchange.com/questions/ 9933 /存在,-A-约定换受理 - 我 - 自己的回答对我 - 拥有 - 问题 – 2012-08-17 17:22:16