将大型xml上载到WCF REST服务 - > 400 Bad request

Joh*_*ann 5 rest wpf wcf

我一直在努力寻找这个错误但到目前为止没有运气.

所以我通过这个web.config在我的客户端上有一个服务

  <system.serviceModel>
<serviceHostingEnvironment>
  <baseAddressPrefixFilters>
    <add prefix="http://www.mywebsite.com/"/>
  </baseAddressPrefixFilters>
</serviceHostingEnvironment>
<services>
  <service behaviorConfiguration="ServiceBehavior" name="UploadService">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
      contract="IUploadService">
      <identity>
        <dns value="http://www.mywebsites.com/" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior" maxReceivedMessageSize="4194304">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

在客户端我有这个配置

  <system.serviceModel>
<bindings>
  <wsHttpBinding>
    <binding name="WSHttpBinding_IUploadService" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="4194304"
      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
      allowCookies="false">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <reliableSession ordered="true" inactivityTimeout="00:30:00"
        enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="">
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
          algorithmSuite="Default" establishSecurityContext="true" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint address="http://www.mywebsite.com/UploadService.svc"
    binding="basicHttpBinding" bindingConfiguration="" contract="IUploadService"
    name="WSHttpBinding_IUploadService">
    <identity>
      <dns value="http://www.mywebsite.com/" />
    </identity>
  </endpoint>
</client>
Run Code Online (Sandbox Code Playgroud)

我正在上传这样的文件: -

        using (Stream stream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
        {
            try
            {
                using (UploadServiceClient upc = new UploadServiceClient())
                {
                    upc.UploadFile(stream);
                }

            }
            catch (Exception exc)
            {

            }
        }
Run Code Online (Sandbox Code Playgroud)

对于小文件,它工作正常,但对于大型XML文件,这会导致400错误请求.我可以做些什么来更改这些设置以获取要传输的大型XML文件?

谢谢你的帮助和时间

UPDATED客户端app.config

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicHttpBinding_IUploadService" receiveTimeout="00:20:00"
      bypassProxyOnLocal="true" maxBufferSize="4194304" maxReceivedMessageSize="4194304"
      messageEncoding="Mtom" transferMode="Streamed">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security>
        <transport>
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://www.mywebsite.com/UploadService.svc"
    binding="basicHttpBinding" bindingConfiguration="" contract="IUploadService"
    name="basicHttpBinding_IUploadService">
    <identity>
      <dns value="http://www.mywebsite.com/" />
    </identity>
  </endpoint>
</client>
Run Code Online (Sandbox Code Playgroud)

Six*_*aez 2

您应该查看服务是否具有与客户端相同的 maxReceivedMessageSize="4194304" 以及 XML 是否确实小于设置的 4,194,304 字节限制。WCF 默认 maxReceivedMessageSize 为 64K。

更新:

我注意到您的配置显示正在为 basicHttpBinding 配置客户端,但该配置仅显示 wsHttpBinding。wsHttpBinding 配置将被 WCF 忽略,因为它与 basicHttpBinding 无关。如果客户端配置文件没有 basicHttpBinding 元素,则在 .NET 4 中将使用默认元素。如果这是真的,那么您将遇到上述 64K 限制。