【问题标题】:Cannot parse MTOM response无法解析 MTOM 响应
【发布时间】:2013-07-26 14:06:17
【问题描述】:

我正在使用第三方 Web 服务,该服务返回的对象带有使用 MTOM 编码附加的 PDF。

对象的结构为Data[],每个数组元素都有ContentTypeInclude字段。

当我运行 Web 服务方法时,它可以很好地完成请求,但它没有正确解析响应,因为 Include 字段被解析为 null

当我运行 Fiddler 时,我实际上可以看到远程 Web 服务返回包含所有可用字段的响应。

这是在 SOAP 中发送的内容:

<m:GetDocImageResponse>
    <x:data>
        <x:item xmime5:contentType="*/*">
        <xop:Include href="cid:id1"/></x:item>
    </x:data>
</m:GetDocImageResponse>

我看到 Include 具有名为 href 的属性,其中包含对二进制 PDF 文档的引用。

我正在尝试根据 WSDL 解析对象:

Data[] retObject = null;
using (blahWS ws = new blahWS())
{
 try{
retObject = ws.GetDoc(parameters); //request completes with no errors, but `Include` is parse as null
[...]
   }
catch
{..}
 }

Web 服务引用与简单的basicHttpBinding 一起使用

<basicHttpBinding>
    <binding name="BasicHTTPwithMTOM" messageEncoding="Mtom" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" textEncoding="utf-8" />        
</basicHttpBinding>

我应该以不同的方式解析响应吗?为什么不解析字段?

编辑:

完整的 SOAP 响应:

HTTP/1.1 200 OK
Server: gSOAP/2.7
Content-Type: multipart/related; charset=utf-8; boundary="==nGpzR/KspN6ry7jG8CU4bonN2aujzfJamyN3xYjaldFXYpeUryNGb0UROC0B=="; type="application/xop+xml"; start="<SOAP-ENV:Envelope>"; start-info="text/xml"
Content-Length: 180557
Connection: close

--==nGpzR/KspN6ry7jG8CU4bonN2aujzfJamyN3xYjaldFXYpeUryNGb0UROC0B==
Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <SOAP-ENV:Envelope>

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:xmime5="http://www.w3.org/2005/05/xmlmime" xmlns:m="http://www.mcleodsoftware.com/wsdl/ws4v.wsdl" xmlns:x="http://www.mcleodsoftware.com/schemas/ws4v.xsd">
<SOAP-ENV:Body>
    <m:GetDocImageResponse>
        <x:data>
            <x:item xmime5:contentType="*/*">
                <xop:Include href="cid:id1"/></x:item>
        </x:data>
    </m:GetDocImageResponse>
</SOAP-ENV:Body>

--==nGpzR/KspN6ry7jG8CU4bonN2aujzfJamyN3xYjaldFXYpeUryNGb0UROC0B==
Content-Type: */*
Content-Transfer-Encoding: binary
Content-ID: <id1>
...binary...

使用提供的 WSDL 构建的数据定义:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.mcleodsoftware.com/schemas/ws4v.xsd")]
public partial class Data : object, System.ComponentModel.INotifyPropertyChanged {

    private Include includeField;

    private string contentTypeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://www.w3.org/2004/08/xop/include", Order=0)]
    public Include Include {
        get {
            return this.includeField;
        }
        set {
            this.includeField = value;
            this.RaisePropertyChanged("Include");
        }
    }

【问题讨论】:

  • 如果使用标准的mtom,那么只要您将消息编码设置为mtom,您就不需要解析,附件将在响应对象中可用
  • 您能否提供整个 SOAP 消息(或者更好的是,除了编码附件之外的整个 MIME 消息?)另外,请提供相关的 ServiceContract / DataContract 定义。您需要确保要进行 MTOM 解码的 DataMember 是 byte[] 类型。
  • @EugeneOsovetsky 我添加了 SOAP 响应和对象定义。我没有自己的数据联系人,我使用的是 WSDL 构建的。

标签: c# wcf mtom


【解决方案1】:

您的 WSDL 似乎明确地为元素提供了架构。我很确定这是错误的并且不符合标准。该元素应简单地声明为 xsd:base64Binary 类型(例如,参见 https://wiki.duraspace.org/display/GSOC/MTOM+Support+on+the+WSDL+Level ),并且在您的 WSDL 中还有其他符合标准的方式表明您正在使用 MTOM(例如,参见 http://www.w3.org/Submission/WS-MTOMPolicy/

如果您修复您的 WSDL 以符合标准(或至少符合 WCF 期望的“MTOM WSDL”外观),我认为一切都应该工作。实际上,如果您想查看正确的“MTOM WSDL”是什么样子,只需在 WCF 中先创建一个简单的 MTOM 服务代码 - 例如http://msdn.microsoft.com/en-us/library/aa395209.aspx - 查看它生成的 WSDL 和 XSD。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2013-08-11
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2011-02-18
    相关资源
    最近更新 更多