.net – Setting MaxReceivedMessageSize in the client of a wcf rest service

netrestwcf

I have a rest WCF service to which I connect using a console application. The console app downloads a file. Small files work fine. For larger files I get the error below:

The maximum message size quota for incoming messages (65536) has been exceeded. 
To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element

This is my config file on the client side console app.

<configuration>     
<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding maxReceivedMessageSize="2000000"
                  maxBufferSize="2000000">
          <readerQuotas maxStringContentLength="2000000"/>
        </binding>
      </webHttpBinding>
    </bindings>
    </system.serviceModel>
</configuration>

The WCF config is as follows:

    <webHttpBinding>
        <binding name="MyTestBinding" maxReceivedMessageSize="10000000" maxBufferPoolSize="10000000" maxBufferSize="10000000" transferMode="Buffered">
            <readerQuotas maxDepth="10000000" maxArrayLength="10000000" maxBytesPerRead="10000000" maxNameTableCharCount="10000000" maxStringContentLength="10000000" />
            <security mode="Transport">
                <transport clientCredentialType="None" />
            </security>
        </binding>
    </webHttpBinding>

I am using WebChannelFactory to connect to the service. What could be wrong?

Best Answer

Try assigning a name to the webHttpBinding in your client config file, and referencing it with the WebChannelFactory Constructor (String, Uri). This takes a string for the binding configuration name and the Uri of the service:

<configuration>
  <system.serviceModel>
    <bindings>
      <webHttpBinding name="MyWebHttpBinding">
        <binding maxReceivedMessageSize="2000000"
                 maxBufferSize="2000000">

basicHttpBinding is the default binding for http, so unless you've overridden that in the client config's protocolMapping section you will get the default values for httpBinding.

With the name attribute set, you could then get a factory instance like this:

WebChannelFactory<IContract> factory = new WebChannelFactory<IContract>("MyWebHttpBinding", new Uri("service address"));
Related Topic