Search code examples
wcfwcf-binding

How to change default message size using ClearUserNameBinding?


We are using ClearUserNameBindig in our WCF service.

When we tried to return a message with more than 3k records, we received this error:

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.

We tried to modify web.config like that:

<bindings>
  <clearUsernameBinding>
    <binding name="myClearUsernameBinding"
              maxReceivedMessageSize="20000000"
              maxBufferSize="20000000"
              maxBufferPoolSize="20000000" />
    <readerQuotas maxDepth="32"
              maxArrayLength="200000000"
            maxStringContentLength="200000000"/>
  </clearUsernameBinding>
</bindings>

But we received this error:

Unrecognized attribute 'maxReceivedMessageSize'.

How to change default message size using ClearUserNameBinding?


Solution

  • We found the solution following this steps:

    http://sureshjakka.blogspot.com.ar/2010/03/changing-message-sizes-in.html

    We modify the code of ClearUserNameBinding like this:

    1. In AutoSecuredHttpTransportElement() constructor, initialize the values to maximum possible

      public AutoSecuredHttpTransportElement()
      {
          MaxReceivedMessageSize = int.MaxValue;
          MaxBufferSize = int.MaxValue;
          MaxBufferPoolSize = long.MaxValue;
      }
      
    2. In CreateBindingElements() method create XMLDictionaryReaderQutotas object and set the same on TextMessageEncodingBindingElement. Here is the modified version of this method.

      public override BindingElementCollection CreateBindingElements()
      {
          XmlDictionaryReaderQuotas rqMax = XmlDictionaryReaderQuotas.Max;
          TextMessageEncodingBindingElement textBE = new TextMessageEncodingBindingElement();
          textBE.MessageVersion = this.messageVersion;
      
          rqMax.CopyTo(textBE.ReaderQuotas);
      
          var res = new BindingElementCollection();
          res.Add(textBE); 
          res.Add(SecurityBindingElement.CreateUserNameOverTransportBindingElement());
          res.Add(new AutoSecuredHttpTransportElement());
          return res;
      }
      

    Note: Make sure that you have "System.Runtime.Serialization" version 3.0.0.0 and above in your references. Because if you have version 2.0.0.0, you will get compile error as this version does not allow setting properties on ReaderQuotas.

    Web.config:

    <bindings>
      <clearUsernameBinding>
        <binding name="myClearUsernameBinding" />
      </clearUsernameBinding>
    </bindings>
    

    Finally We update the references in server and client.