Rakin
Rakin

Reputation: 1279

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>

Upvotes: 8

Views: 9125

Answers (7)

Sachin Gaikwad
Sachin Gaikwad

Reputation: 340

Try increasing the "maxItemsInObjectGraph" size in web.config file, as this change worked for me.For details please see.

Upvotes: 2

Lewis Hai
Lewis Hai

Reputation: 1214

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!

Upvotes: 1

Josh
Josh

Reputation: 1049

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.

Upvotes: 1

Ricky Alexandersson
Ricky Alexandersson

Reputation: 94

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.

Upvotes: 2

Mimas
Mimas

Reputation: 523

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

Upvotes: 3

Zwan
Zwan

Reputation: 642

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>

Upvotes: 3

Zwan
Zwan

Reputation: 642

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

Upvotes: 4

Related Questions