简体   繁体   中英

Unexpected response returned by WCF Service: (413) Request Entity Too Large

I've implemented a small set of REST services using WCF. One of the services recieves a large amount of data. When calling it (this is when runnig it from visual studio - I haven't deployed itto a production server yet) I get the error

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

My web config

<binding name="BasicHttpBinding_ISalesOrderDataService" 
         closeTimeout="00:10:00"
         openTimeout="00:10:00" 
         receiveTimeout="00:10:00" 
         sendTimeout="00:10:00"
         allowCookies="false" 
         bypassProxyOnLocal="false" 
         hostNameComparisonMode="StrongWildcard"
         maxBufferPoolSize="2147483647" 
         maxBufferSize="2147483647" 
         maxReceivedMessageSize="2147483647"
         textEncoding="utf-8" 
         transferMode="Buffered" 
         useDefaultWebProxy="true"
         messageEncoding="Text">
  <readerQuotas maxDepth="2000000" 
                maxStringContentLength="2147483647"
                maxArrayLength="2147483647" 
                maxBytesPerRead="2147483647"
                maxNameTableCharCount="2147483647" />
  <security mode="None">
    <transport clientCredentialType="None" 
               proxyCredentialType="None" 
               realm="" />
    <message clientCredentialType="UserName" 
             algorithmSuite="Default" />
  </security>
</binding>

Seem you exceed quota augment those value.

 maxReceivedMessageSize="2000000" maxBufferSize="2000000">

(or review your query for a lower result when possible)

if nothing work check here its like comon probleme.

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

I'm afraid your client is fine but you need to check the server web.config

add value the same way you did for your client

<bindings>
      <basicHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
</bindings>

In addition to increasing message size and buffer size quotes, consider also increase maxItemsInObjectGraph for serializer. It can matter if your object has complex structure or array of objects inside. Our typical setting look so

 <behaviors>
  <endpointBehaviors>
    <behavior name="GlobalEndpoint">
      <dataContractSerializer maxItemsInObjectGraph="1365536" />
    </behavior>
 </behaviors>
 <serviceBehaviors>
    <behavior name="GlobalBehavior">
      <dataContractSerializer maxItemsInObjectGraph="1365536" />
    </behavior>
 </serviceBehaviors>

And additionaly what Zwan has proposed

If I understand you correctly your request is the one that is delivering large amounts of data. That means that you have to edit the maxRecievedMessageSize like @Zwan have written. Not in the client's config but in the rest services config to allow big requests of data.

尝试增加web.config文件中的“maxItemsInObjectGraph”大小,因为此更改对我有用 。有关详细信息,请参阅。

If you host the wcf rest service in a asp.net application, the httpRuntime limits must also be set because wcf service is running in ASP .NET compatibilty mode. Note that maxRequestLength has value in kilobytes

<configuration> <system.web>
<httpRuntime maxRequestLength="10240" /> </system.web> </configuration>

Refer at The remote server returned an error: (413) Request Entity Too Large

More advice should make Dispose, destructor for services

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple, MaxItemsInObjectGraph = 2147483647)]
[GlobalErrorBehaviorAttribute(typeof(GlobalErrorHandler))]
public partial class YourService : IYourService
{

    // Flag: Has Dispose already been called? 
    bool disposed = false;
    // Instantiate a SafeHandle instance.
    SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);

    // Public implementation of Dispose pattern callable by consumers. 
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    // Protected implementation of Dispose pattern. 
    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
            return;

        if (disposing)
        {
            handle.Dispose();
            // Free any other managed objects here. 
            //
        }

        // Free any unmanaged objects here. 
        //
        disposed = true;
    }

    ~YourService()  // destructor
    {
      Dispose();
    }

}

Hope it helps!

Since ok with maxRecievedMessageSize, you can check "IIS Request Filtering" where maximum length of content in a request, in bytes

Also in the IIS – “UploadReadAheadSize” that prevents upload and download of data greater than 49KB. The value present by default is 49152 bytes and can be increased up to 4 GB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM