配置 WCF 服务服务器端以接受 Mtom

Configure WCF service server side to accept Mtom

我对 Web 服务还很陌生,我正在尝试配置我的 WCF 服务以使用 Visual WCF 服务模板从另一个服务中提取文件。

除了内容数据,我已经设法提取了所有内容。我已经搜索并发现我必须使用 Mtom 而不是 Text 进行消息编码。问题是我不确定如何配置我的 Web 服务以使用 MessageEncoding = Mtom。我摆弄了 web.config 文件,但我似乎无法正确配置它。

这是我的服务界面

 [ServiceContract]
public interface IService
{

    [OperationContract]
    string test(changeData data);
    [OperationContract]
    RetrieveChangeListResponse GetChangeList();
    [OperationContract]
    RetrieveChangeTaskListResponse GetTaskList();
    [OperationContract]
    RetrieveInteractionAttachmentResponse GetFile(string id);

    // TODO: Add your service operations here
}

这是我的web.config

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000"
             messageEncoding ="Mtom">
      <readerQuotas maxDepth="32"
           maxArrayLength="200000000"
           maxStringContentLength="200000000"/>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="IService"
           behaviorConfiguration="ServiceWithMetadata">
    <endpoint name="BasicHttpBinding_IService"
              binding="basicHttpBinding"
              bindingConfiguration="basicHttp"
              contract="IService" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceWithMetadata">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

目前如果使用这个设置我会得到这个错误:

Error: Cannot obtain Metadata from localhost:50282/Service.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at URI: localhost:50282/Service.svc Metadata contains a reference that cannot be resolved: 'localhost:50282/Service.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service localhost:50282/Service.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..HTTP GET Error URI: localhost:50282/Service.svc The HTML document does not contain Web service discovery information.

如果我删除这些设置,我会得到这样的类型不匹配:

Client found response content type of 'multipart/related; type="text/xml"; boundary="----=_Part_0_30957184.1421681490926"', but expected 'text/xml'.

我知道有 WcfTestClient 的配置和实际服务的配置,但我不明白如何正确配置我的服务器端以正确接受 Mtom 编码。

有人可以解释一下如何正确配置服务器端绑定吗?我使用 BasicHttpBinding,但也想使用 wshHttpBinding 以获得 soap 1.2 功能。

提前致谢。

问题出在所使用的身份验证方法中,我使用的是匿名而不是基本的。 此外,您不能拥有作为代理数据类型的方法 return 类型。如果你有这样的方法,你会得到关于 class 推导的错误。