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

时间:2013-06-06 19:11:14

标签: c# mtom

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

Client found response content type of 'multipart/related; boundary="MIMEBoundaryurn_uuid_5D7E9AC12266BFFBDB1370543444063"; start-info="text/xml"; type="text/xml"; start="<0.urn:uuid:5D7E9AC12266BFFBDB1370543444064@apache.org>"', but expected 'text/xml'.

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

示例:

<?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>

矿:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=----------------" >
          <section name="TWS.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=----------------" requirePermission="false" />
        </sectionGroup>
      </configSections>
      <applicationSettings>
        <TWS.Properties.Settings>
          <setting name="TWS_WebService_WsApiService" serializeAs="String">
            <value>https://www.<redacted>.com/services/WsApiService/</value>
          </setting>
        </TWS.Properties.Settings>
      </applicationSettings>
    </configuration>

以下是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]));
        }

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

1 个答案:

答案 0 :(得分:0)

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