背景信息:在 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 二进制文件?