C# SOAP/MTOM:VS生成的代理类和处理PDF附件的问题

C# SOAP/MTOM:VS生成的代理类和处理PDF附件的问题,c#,mtom,C#,Mtom,背景信息:在C#中构建Web服务消费客户端。使用svcutil自动生成代理类。web服务设置为通过MTOM发送PDF附件,当我使用来自代理的API调用尝试发送PDF附件时,出现以下错误: 客户端发现响应内容类型为“多部分/相关”;boundary=“MIMEBoundaryurn_uuid_5d7e9ac1226bffbdb137054344063”;start info=“text/xml”;type=“text/xml”;start=“”,但应为“text/xml”。 …我发现这意味着客户端

背景信息:在C#中构建Web服务消费客户端。使用svcutil自动生成代理类。web服务设置为通过MTOM发送PDF附件,当我使用来自代理的API调用尝试发送PDF附件时,出现以下错误:

客户端发现响应内容类型为“多部分/相关”;boundary=“MIMEBoundaryurn_uuid_5d7e9ac1226bffbdb137054344063”;start info=“text/xml”;type=“text/xml”;start=“”,但应为“text/xml”。

…我发现这意味着客户端没有正确配置MTOM。好的,很酷,这里有一些如何更改配置的示例。不过,这里是我绊倒的地方——到目前为止,我发现的所有示例的配置文件映射方式都与我的不同。示例在
system.serviceModel
部分中设置了两项,一项用于绑定,另一项用于包含端点地址的客户端端点。我自动生成的配置将地址插入到
应用程序设置中
,我很困惑:(在这里,它们并排排列:

示例:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IUpload" messageEncoding="Mtom"/>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/ServiceModelSamples/service.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IUpload" contract="IUpload">
      </endpoint>
    </client>
  </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

地雷:


https://www..com/services/WsApiService/
下面是如何在代理类中设置API调用本身:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/getStiDoc", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        [return: System.Xml.Serialization.XmlElementAttribute("doc", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="base64Binary", IsNullable=true)]
        public byte[] getDoc([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=true)] string id) {
            object[] results = this.Invoke("getDoc", new object[] {
                        username,
                        id});
            return ((byte[])(results[0]));
        }
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(“http://localhost:8080/getStiDoc,RequestNamespace=http://www..com/ws/schemas,ResponseNamespace=http://www..com/ws/schemas,Use=System.Web.Services.Description.SoapBindingUse.Literal,ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[返回:System.Xml.Serialization.xmlementAttribute(“doc”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified,DataType=“base64Binary”,IsNullable=true)]
公共字节[]getDoc([System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]字符串用户名,[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,DataType=“integer”,IsNullable=true)]字符串id){
object[]results=this.Invoke(“getDoc”,新对象[]{
用户名,
id});
返回((字节[])(结果[0]);
}

我的问题是:如何设置配置文件,我应该如何添加适当的绑定以确保它查找MTOM而不是base64二进制?

最后我自己找到了答案。为了正确设置代理类(即以符合WCF的形式),我必须通过命令行使用svcutil将WSDL转换为代理类。在我这样做之后,我能够将消息绑定设置为MTOM。我仍然无法使用证书对其进行身份验证,但这是一个较小的问题

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/getStiDoc", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        [return: System.Xml.Serialization.XmlElementAttribute("doc", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="base64Binary", IsNullable=true)]
        public byte[] getDoc([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=true)] string id) {
            object[] results = this.Invoke("getDoc", new object[] {
                        username,
                        id});
            return ((byte[])(results[0]));
        }