SoapHttpClientProtocol 接收意外的内容类型

本文关键字:类型 意外 SoapHttpClientProtocol | 更新日期: 2023-09-27 18:33:47

我通过wsdl.exeWSDL -URL创建了一个C#代理类。我在无法控制的 Web 应用程序的上下文中使用此代理类(因此无法更改web.conf或类似内容)。我也无法更改我正在与之交谈的 Web 服务中的任何内容。

调用 Web 服务时,我收到以下异常:

Client found response content type of 'multipart/related; type="application/xop+xml"; 
boundary="uuid:5c314128-0dc0-4cad-9b1a-dc4a3e5917bb"; start="<root.message@cxf.apache.org>"; 
start-info="application/soap+xml"', but expected 'application/soap+xml'.

从我所读到的内容来看,这是 Web 服务使用 MTOM 的问题。现在我试图告诉我的班级接受 MTOM,但我发现的只是web.conf中的配置。

代理类派生自SoapHttpClientProtocol,如下所示(相关部分):

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]    
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name = "myrequestSoapBinding", Namespace = "http://namespace.of.company.of.webservice/")]
public class myrequest : System.Web.Services.Protocols.SoapHttpClientProtocol
{
    public myrequest(string url)
    {
        this.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;            
        this.Url = url;
    }
    [return: System.Xml.Serialization.XmlElementAttribute("return", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "base64Binary")]
    public byte[] getDocuments([System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "base64Binary")] byte[] input)
    {
        try
        {
            object[] results = this.Invoke("getDocuments", new object[] {
                input});
            return ((byte[])(results[0]));
        }
        catch (Exception ex)
        {
            var realResponse = StripResponse(ex.Message);
            return Encoding.UTF8.GetBytes(realResponse.ToString());
        }
    }
}

getDocuments 中的try ... catch是一种笨拙的解决方法,它可以从异常Message中获取"真正的"服务响应 - 这不是我真正想要实现它的方式。

所以我的问题是:有没有办法更改代理类中的绑定以接受MTOM响应?

SoapHttpClientProtocol 接收意外的内容类型

从我为提供帮助所做的少量研究来看,如果您确实可以访问Web配置(我知道您没有)并且打开了MTOM,那么Visual Studio将生成两个代理类:

  1. 一个派生自SoapHttpClientProtocol的标准版本,以及;
  2. 一个 WSE 一个 WSE,在派生自 Microsoft.Web.Services3.WebServicesClientProtocol 的类名后附加了"Wse"

它是能够接受MTOM的WebServicesClientProtocol实现。 若要让 WSDL 创建派生自 WebServicesClientProtocol 的代理,请按照此 MSDN 文章顶部的说明进行操作。

希望这将解决问题。