24

I got this crazy problem I'm trying to deal with. I know that when we're getting huge amount of data we must increase the quota on client .config file, but what am I supposed to do if my client is sending huge data "to" the WCF server?

It works perfectly normal when I'm sending small sized input parameter. Unfortunately, it breaks down when the input grows bigger.

Debugger it says:

Bad Request, 400;

on trace file it is:

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.

Is there some way to increase this quota of incomming data on the server side? if so, how?

Here's my sample config related parts:

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

  </basicHttpBinding>
</bindings>

 <services>
  <service name="MyWcfService">
    <endpoint address="http://myservice..."
      binding="basicHttpBinding" bindingConfiguration="MyBasicHttpBinding"
      name="MyBasicHttpBinding" contract="IMyContract" />
  </service>
</services> 

and here's my client-side code (I create it dynamically):

        var binding = new BasicHttpBinding();
        binding.MaxBufferPoolSize = 2147483647;
        binding.MaxBufferSize = 2147483647;
        binding.MaxReceivedMessageSize = 2147483647;
        binding.ReaderQuotas.MaxStringContentLength = 2147483647;
        binding.ReaderQuotas.MaxArrayLength = 2147483647;
        binding.ReaderQuotas.MaxDepth = 2147483647;
        binding.ReaderQuotas.MaxBytesPerRead = 2147483647;


        var address = new EndpointAddress("http://mylocalservice.."); 

        ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>(binding, address);

        foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
        {
            DataContractSerializerOperationBehavior dataContractBehavior =
                        op.Behaviors.Find<DataContractSerializerOperationBehavior>()
                        as DataContractSerializerOperationBehavior;
            if (dataContractBehavior != null)
            {
                dataContractBehavior.MaxItemsInObjectGraph = 2147483646;
            }
        }
        public IMyContract MyClientObject = factory.CreateChannel();
tshepang
  • 12,111
  • 21
  • 91
  • 136
rockin'
  • 749
  • 3
  • 8
  • 16
  • 1
    I solved the problem by the step mentioned [here][1] [1]: http://stackoverflow.com/questions/7476853/wcf-error-maximum-number-of-items-that-can-be-serialized-or-deserialized-in-an/8656402#8656402 – Ram Dec 28 '11 at 13:28
  • 1
    In your client side code you are setting the readerquotas via code so the below is the approach to set them : XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = 2147483647; myReaderQuotas.MaxArrayLength = 2147483647; myReaderQuotas.MaxBytesPerRead = 2147483647; myReaderQuotas.MaxDepth = 64; myReaderQuotas.MaxNameTableCharCount = 2147483647; binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); – Rajesh Jul 05 '13 at 16:21
  • Does this answer your question? [The maximum message size quota for incoming messages (65536) has been exceeded](https://stackoverflow.com/questions/5459697/the-maximum-message-size-quota-for-incoming-messages-65536-has-been-exceeded) – KyleMit Nov 05 '19 at 17:25

3 Answers3

47

You can set the MaxReceivedMessageSize property on the service via the service's config file.

Setting the client's MaxReceivedMessageSize only affects messages received by the client; it has no effect on messages received by the service. Correspondingly, to allow the service to receive large messages, you need to set the service's config file.

Sample Service Config

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="MyBinding" maxReceivedMessageSize="2147483647" />
    </wsHttpBinding>
  </bindings>
  <services>
    <service name="MyService">
      <endpoint address="http://myservice" binding="wsHttpBinding"
                bindingConfiguration="MyBinding" contract="IMyServiceContract" />
    </service>
  </services>
</system.serviceModel>

The above is a very stripped down config file, but shows the relevant parts. The important part is that you define the binding and give it a name, and set any values explicitly that are different from the defaults, and use that name in the bindingConfiguration attribute on the endpoint element.

Tim
  • 28,212
  • 8
  • 63
  • 76
  • the thing is I don't see any binding configuration on server side. is there some default binding or something? how can I override it? – rockin' Dec 05 '11 at 08:06
  • 1
    Yes, there is. If you don't have a specific binding section in your config file, and it's not referenced in the `` element via the bindingConfiguration attribute, the default values for the binding will be used (which is 65536 for MaxReceivedMessageSize property). So you'll need to add the appropriate `` section to the config. – Tim Dec 05 '11 at 08:15
  • is there any kind of example of server side binding and/or endpoint configuration? I'm already stuck at that point. – rockin' Dec 05 '11 at 08:33
  • @rockin' - Post your service's config file, please. – Tim Dec 05 '11 at 15:32
  • @rockin' - the config file looks right. How are you creating the proxy on the client? Can you add that bit of code to your question? Depending on how you're creating it, that might be where the problem lies. I can probably tell for sure once I see the code. – Tim Dec 06 '11 at 00:34
  • How to do the setting at client's MaxReceivedMessageSize when the client is MS Excel? – Anil Soman May 08 '14 at 07:47
  • Hi Everyone, Is there any alternative ways to communicate large data b/w client and server – Rajendar Manda Sep 19 '17 at 09:44
7

My problem was with the wcf test client. For some reason, it wouldn't generate a client config with the increased maxReceivedMessageSize. My fix was to right click "Config file" -> "Edit with SvcConfigEditor" -> bindings -> maxReceivedMessageSize.

thebiggestlebowski
  • 2,610
  • 1
  • 33
  • 30
  • in most answers they show how to set these settings on the server. but if still error then, as you mentioned, we should also config these settings on the client. in my case , the app.config of the application that consumes the webservice first looked like this: I modified like this (see part 2): – mihai71 May 17 '17 at 10:23
  • ... part 2: and it worked ok. – mihai71 May 17 '17 at 10:23
6

This issue can be resolved by adding the below additional binding node to the binding section of config file.

<basicHttpBinding>
  <binding maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
       maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
  </binding>
</basicHttpBinding>
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
Amit S
  • 69
  • 1
  • 1
  • 4
    Welcome to StackOverflow. The OP has a very similar code in their config. In which way is your answer better than what they had? You can improve your answer by explaining answer does that they didn't do before. – carlosfigueira Aug 06 '13 at 22:50