WCF Issue: The remote server returned an error: (413) Request Entity Too Large

WCF Issue: The remote server returned an error: (413) Request Entity Too Large

我知道这个问题已经被问过很多次了,但不幸的是 none 他们解决了我的问题。

我有一个 wcf 网络服务,我可以在其中发送字节形式的文件。如果文件很大(大约 400 KB),则会出现此错误。对于小文件,这个问题不会出现。以下是我的服务器端点配置,

<service behaviorConfiguration="CPServicesBehaviour" name="ConsumerPortalAPI.ReceiveNoteService">
  <endpoint address="" binding="customBinding" bindingConfiguration="CustomBinding_IReceiveNoteService" contract="ConsumerPortalAPI.IReceiveNoteService" />
</service>

绑定配置是,

<binding name="CustomBinding_IReceiveNoteService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
  <security defaultAlgorithmSuite="Default" authenticationMode="SecureConversation" requireDerivedKeys="false" includeTimestamp="false" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
  canRenewSecurityContextToken="true">
    <secureConversationBootstrap defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport" requireDerivedKeys="true" includeTimestamp="true" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
      <localClientSettings detectReplays="false" />
      <localServiceSettings detectReplays="false" />
    </secureConversationBootstrap>
    <localClientSettings detectReplays="false" />
    <localServiceSettings detectReplays="false" />
  </security>
  <binaryMessageEncoding>
    <readerQuotas maxDepth="32767" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="32767" maxNameTableCharCount="2147483647" />
  </binaryMessageEncoding>
  <httpsTransport />
</binding>

默认绑定是,

<binding maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
  <security defaultAlgorithmSuite="Default" authenticationMode="SecureConversation" requireDerivedKeys="false" includeTimestamp="false" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
  canRenewSecurityContextToken="true">
    <secureConversationBootstrap defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport" requireDerivedKeys="true" includeTimestamp="true" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
      <localClientSettings detectReplays="false" />
      <localServiceSettings detectReplays="false" />
    </secureConversationBootstrap>
    <localClientSettings detectReplays="false" />
    <localServiceSettings detectReplays="false" />
  </security>
  <binaryMessageEncoding>
    <readerQuotas maxDepth="32767" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="32767" maxNameTableCharCount="2147483647" />
  </binaryMessageEncoding>
  <httpsTransport />
</binding>

在我的测试客户端配置是,

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomBinding_IReceiveNoteService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
          <security defaultAlgorithmSuite="Default" authenticationMode="SecureConversation" requireDerivedKeys="false" includeTimestamp="false" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
          canRenewSecurityContextToken="true">
            <secureConversationBootstrap defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport" requireDerivedKeys="true" includeTimestamp="true" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
              <localClientSettings detectReplays="false" />
              <localServiceSettings detectReplays="false" />
            </secureConversationBootstrap>
            <localClientSettings detectReplays="false" />
            <localServiceSettings detectReplays="false" />
          </security>
          <binaryMessageEncoding />
          <httpsTransport />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="https://cnsportal3.lineartitle.com/cnsAPI/v4/ReceiveNoteService.svc" binding="customBinding" bindingConfiguration="CustomBinding_IReceiveNoteService" contract="CPAction.IReceiveNoteService" name="CustomBinding_IReceiveNoteService" />
    </client>
  </system.serviceModel>
</configuration>

我尝试将我网站的 uploadReadAheadSize 增加到 10485759。 我还尝试将我网站的 maxAllowedContentLength 增加到 3000000000。

我通过system.diagnostics检查了服务器上的网络跟踪。以下是跟踪中记录的 Xml 消息,

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
  <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
    <EventID>524326</EventID>
    <Type>3</Type>
    <SubType Name="Information">0</SubType>
    <Level>8</Level>
    <TimeCreated SystemTime="2015-01-21T11:01:33.3751197Z" />
    <Source Name="System.ServiceModel" />
    <Correlation ActivityID="{90d52ffb-d886-4164-9d84-e74c04eb79b4}" />
    <Execution ProcessName="w3wp" ProcessID="5916" ThreadID="16" />
    <Channel />
    <Computer>RES-DEV-WEB</Computer>
  </System>
  <ApplicationData>
    <TraceData>
      <DataItem>
        <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information">
          <TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.GetServiceElement.aspx</TraceIdentifier>
          <Description>Get ServiceElement.</Description>
          <AppDomain>/LM/W3SVC/5/ROOT/cnsAPI/v4-2-130663116927511157</AppDomain>
          <ExtendedData xmlns="http://schemas.microsoft.com/2006/08/ServiceModel/ServiceConfigurationTraceRecord">
            <FoundServiceElement>True</FoundServiceElement>
            <ConfigurationFileSource>C:\inetpub\wwwroot\cnsportal3.lineartitle.com\ConsumerPortalAPI\v4\web.config</ConfigurationFileSource>
            <ConfigurationFileLineNumber>133</ConfigurationFileLineNumber>
            <ServiceConfigurationInformation>
              <ServiceName>ConsumerPortalAPI.ReceiveNoteService</ServiceName>
              <BehaviorConfiguration>CPServicesBehaviour</BehaviorConfiguration>
              <Host>
                <Timeouts>
                  <OpenTimeout>00:01:00</OpenTimeout>
                  <CloseTimeout>00:00:10</CloseTimeout>
                </Timeouts>
              </Host>
              <Endpoints>
                <Endpoint>
                  <Binding>customBinding</Binding>
                  <BindingConfiguration>CustomBinding_IReceiveNoteService</BindingConfiguration>
                  <Contract>ConsumerPortalAPI.IReceiveNoteService</Contract>
                  <ListenUriMode>Explicit</ListenUriMode>
                </Endpoint>
              </Endpoints>
            </ServiceConfigurationInformation>
          </ExtendedData>
        </TraceRecord>
      </DataItem>
    </TraceData>
  </ApplicationData>
</E2ETraceEvent>

貌似可以获取到正确的Binding Configuration,但不知道为什么没有使用。

P.S。所有绑定都在 customBinding 标签下定义。

在这方面的任何帮助将不胜感激。也许我在这里遗漏了一些非常小的东西,你们中的任何一个都可以指出正确的方向。

提前致谢。

我能够通过在绑定配置中添加以下行来解决问题。

<httpsTransport maxReceivedMessageSize="2147483647"/>

整个绑定配置现在看起来像,

<binding name="CustomBinding_IReceiveNoteService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
     <security defaultAlgorithmSuite="Default" authenticationMode="SecureConversation" requireDerivedKeys="false" includeTimestamp="false" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" canRenewSecurityContextToken="true">
            <secureConversationBootstrap defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport" requireDerivedKeys="true" includeTimestamp="true" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
              <localClientSettings detectReplays="false" />
              <localServiceSettings detectReplays="false" />
            </secureConversationBootstrap>
            <localClientSettings detectReplays="false" />
            <localServiceSettings detectReplays="false" />
          </security>
          <binaryMessageEncoding>
            <readerQuotas maxDepth="32767" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="32767" maxNameTableCharCount="2147483647" />
          </binaryMessageEncoding>
          <httpsTransport maxReceivedMessageSize="2147483647"/>
     </binding>