WCF - How to Increase Message Size Quota
Asked Answered
G

13

493

I have a WCF Service which returns 1000 records from database to the client. I have an ASP.NET WCF client (I have added service reference in asp.net web application project to consume WCF).

I get the following message when I run the client application:

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.

Any help? How to increase message size quota?

Gary answered 19/5, 2009 at 18:4 Comment(5)
I had the same problem but I just got an unfriendly network error 400 but the solution was the message size thingie..Joyjoya
I solved the problem by using the steps mentioned in [the link][1] [1]: #7477353Pacific
Why is it set so low by default? Security?Cylindroid
@Coops for security indeed. By setting a quota on the messages for example DDOS attacks are (at least a bit) harder to execute.Dariadarian
Just need to update your config on server sideAmalea
A
649

You'll want something like this to increase the message size quotas, in the App.config or Web.config file:

<bindings>
    <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000" 
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
            <readerQuotas maxDepth="32" 
                 maxArrayLength="200000000"
                 maxStringContentLength="200000000"/>
        </binding>
    </basicHttpBinding>
</bindings>

And use the binding name in your endpoint configuration e.g.

...
bindingConfiguration="basicHttp"
...

The justification for the values is simple, they are sufficiently large to accommodate most messages. You can tune that number to fit your needs. The low default value is basically there to prevent DOS type attacks. Making it 20000000 would allow for a distributed DOS attack to be effective, the default size of 64k would require a very large number of clients to overpower most servers these days.

Attlee answered 19/5, 2009 at 18:6 Comment(23)
Thanks..This change is needs to be made in web.config file of client application.Gary
You may also need to change it on the server -- in the event you need to send in a large dataset as a parameter to a WCF Method.Attlee
Any particular reason for the message size of 20000000?Fimble
Its sufficently large to accomidate most messages. You can tune that number to fit your needs. It is basically there to prevent DOS type attacks. Making it 20000000 would allow for a distributed DOS attack to be effective, the default size of 64k would require a very large number of clients to overpower most servers these day.sAttlee
For others who are interested, I read on another blog that the maximum size is 2147483647. 20000000 is a bit smaller than this number, so using the smallest number you can get away with without interrupting service makes sense.Flatfoot
It doesn't make sense that this is a DOS attack prevention. Wouldn't malicious clients just set this to a high value? Can't I change it on the server instead?Interlocutory
@NeilBarnwell This is a configuration option on both the client and the server.Attlee
@Attlee how can this be configured in extension less hostingBernadette
@Joshua I'm afraid I don't understand your question. Could you elaborate a little on what you're trying to do and what isn't working? Possibly open a new question and link it here?Attlee
With the exception message in the question (about message sizes incoming on the server) the only thing that needs to be changed to fix this particular problem is increasing maxReceivedMessageSize in the server's config file. No change in client config is necessary nor changing any other parameter.Phrixus
@Phrixus It would need to be changed on the server if that incoming parameter was too large; otherwise (and more likely) the change needs to be made in the client configuration file, since it is the response of the service (not its parameter) that is too large.Attlee
Nate, you are right, I see now. The message in the question is probably thrown by the client, not by the server. I was confused because I had exactly the same message, but thrown on server side (because a request parameter was too large).Phrixus
I used this to solve my problem. thanks for the answer. but after the modification when I'm invoking the service using WCF Test client the same error is occurring. but the application is working as i expected. please may i know the reason..?Datnow
Its possible that you are recieving more data than you want. Since you arent using the out of bounds data,its behaving correctly. Just a thought. If you are selecting many entries out of a database and only manipulating the first few, this could be the case.Pram
As @Gab pointed out: "Don't forget that the app.config of the execution entry point will be considered, not the one in class library project managing Web-Service calls if there is one. For example if you get the error while running unit test, you need to set up appropriate config in the testing project."Denney
I also changed to maximum size 2147483647, but still getting the same error any help please ?Jeopardy
Done all the changes as state above but getting the same error. Please help, I am stuck hereIona
I test your solution for my problem but I don't get answer!! https://mcmap.net/q/75395/-wcf-http-bindingEldwon
for java client where i add thisDerogate
i am using c# wcf service in my java application in that where i configure this MaxBufferSizeDerogate
@Derogate This is something configured on the server, if you are consuming the service its not something you will be able to change.Attlee
@Attlee I set the code you provided on my web.config. Where do I set the bindingConfiguration?Subjective
I came back to say that the bindingConfiguration="basicHttp" is the exact answer people should be looking forTours
M
167

If you're still getting this error message while using the WCF Test Client, it's because the client has a separate MaxBufferSize setting.

To correct the issue:

  1. Right-Click on the Config File node at the bottom of the tree
  2. Select Edit with SvcConfigEditor

A list of editable settings will appear, including MaxBufferSize.

Note: Auto-generated proxy clients also set MaxBufferSize to 65536 by default.

Mammillate answered 5/8, 2010 at 6:11 Comment(6)
Why oh why do I always forget about this? +1Brinkmanship
On vs2013 SvcConfigEditor is replace with Edit WCF configuration if people are looking for it.Courtneycourtrai
Not able to find SVCconfigEditor?Goodkin
You will find it under Bindings folder, click on the binding for service and it's in there.Katharyn
If your config file is auto generated, you absolutely should do it this way. Any time you update your reference it will regen the app.config, and you will have to manually change it again. If you change it VS though, the new changes will accommodate the settings you have chosen.Pram
I had to set ReaderQuotas properties as well in the same section underneath.Resh
N
118

If you are creating your WCF bindings dynamically here's the code to use:

BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.MaxReceivedMessageSize = Int32.MaxValue;
httpBinding.MaxBufferSize = Int32.MaxValue;
// Commented next statement since it is not required
// httpBinding.MaxBufferPoolSize = Int32.MaxValue;
Necessaries answered 2/2, 2011 at 10:7 Comment(2)
You can use it initialize. Clearly you can use it your constructor method.Okelley
after 10 years, I need this again, I wish I could upvote twice.Telophase
G
49

The WCF Test Client has it's own client config.

Run the test client and scroll to the bottom. If you double click the Config File node you will see the XML representation. As you can see the maxReceivedMessageSize is 65536.

To edit this, Right Click the Config File tree node and select Edit With SvcConfigEditor. When the editor opens expand Bindings and double click the binding that was automatically generated.

You can edit all the properties here, including maxReceivedMessageSize. When you are done click File - Save.

Lastly, when you are back at the WCF Test Client window, click Tools - Options.

NOTE: Uncheck the Always regenerate config when launching services.

Gaza answered 27/6, 2012 at 20:58 Comment(6)
Probably the best answer here!Ocrea
upvote because of the note to uncheck the Always regenerate config option.Giselegisella
Simplest solve in my opinion. Saved me some headache.Neukam
On vs2013 SvcConfigEditor is replace with Edit WCF configuration if people are looking for it.Courtneycourtrai
Thank you. Been breaking my head for a while, changing the server config again and again, when the problem was with the Test Client config!Diplopod
If I change the maxReceivedMessageSize to 100.000, then refresh the service, after unchecking what u wrote in the note, it still says received message size exceeds 65536. Seems like I cant change the size, even if I save it correctly.Devitrify
A
25

I found the easy way

--- right click the webconfig or app config file and click EDIT WCF CONFIGURATION and got to bingdigs ans select yore service and right side show maxReciveMessageSize give a large number ---

Allantoid answered 30/3, 2015 at 6:39 Comment(1)
It was a great answer, I didnt know that I can edit from here , ThanksMho
F
8

I solve the problem ...as follows

    <bindings>
  <netTcpBinding>
    <binding name="ECMSBindingConfig" closeTimeout="00:10:00" openTimeout="00:10:00"
      sendTimeout="00:10:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
      maxReceivedMessageSize="2147483647" portSharingEnabled="true">
      <readerQuotas maxArrayLength="2147483647" maxNameTableCharCount="2147483647"
          maxStringContentLength="2147483647" maxDepth="2147483647"
          maxBytesPerRead="2147483647" />
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ECMSServiceBehavior">
      <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceTimeouts transactionTimeout="00:10:00" />
      <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="100"
        maxConcurrentInstances="100" />
    </behavior>
  </serviceBehaviors>
</behaviors>
Fetal answered 22/3, 2011 at 5:36 Comment(3)
How does this differ from my solution? Other than you included all the irrelevant portions of your configuration as well as the relevant parts and you chose the maximum possible value instead of the 200m I chose?Attlee
Context is good too... maybe these two answers could be merged?Sidetrack
is that setting be configure in server or client?Amphibole
T
8

I solved my issue on Bing Maps WPF on my project Using CalculateRoute (). The solution in my case was setting the maxReceivedMessageSize and maxReceivedMessageSize on attribute "httpTransport" for "customBinding" section .

I set in the applications.config file (es. myApp.config) this configuration:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IGeocodeService" />
            <binding name="BasicHttpBinding_IRouteService" />
        </basicHttpBinding>
        <customBinding>
            <binding name="CustomBinding_IGeocodeService">
                <binaryMessageEncoding />
              <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                                maxReceivedMessageSize="2147483647" allowCookies="false" authenticationScheme="Anonymous"
                                bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                                keepAliveEnabled="true" maxBufferSize="2147483647" proxyAuthenticationScheme="Anonymous"
                                realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                                useDefaultWebProxy="true" />
            </binding>
            <binding name="CustomBinding_IRouteService">
                <binaryMessageEncoding />
              <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                                maxReceivedMessageSize="2147483647" allowCookies="false" authenticationScheme="Anonymous"
                                bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                                keepAliveEnabled="true" maxBufferSize="2147483647" proxyAuthenticationScheme="Anonymous"
                                realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                                useDefaultWebProxy="true" />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
            contract="BingServices.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
        <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/binaryHttp"
            binding="customBinding" bindingConfiguration="CustomBinding_IGeocodeService"
            contract="BingServices.IGeocodeService" name="CustomBinding_IGeocodeService" />
        <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRouteService"
            contract="BingServices.IRouteService" name="BasicHttpBinding_IRouteService" />
        <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc/binaryHttp"
            binding="customBinding" bindingConfiguration="CustomBinding_IRouteService"
            contract="BingServices.IRouteService" name="CustomBinding_IRouteService" />
    </client>
</system.serviceModel>
Trocki answered 11/5, 2013 at 0:7 Comment(0)
C
8

Another important thing to consider from my experience..

I would strongly advice NOT to maximize maxBufferPoolSize, because buffers from the pool are never released until the app-domain (ie the Application Pool) recycles.

A period of high traffic could cause a lot of memory to be used and never released.

More details here:

Cowes answered 24/10, 2016 at 5:27 Comment(2)
that's very useful, but are there alternatives in theory?Osteoarthritis
Unfortunately I haven't found any alternative.Cowes
C
8

For HTTP:

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000" 
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="200" 
             maxArrayLength="200000000"
             maxBytesPerRead="4096"
             maxStringContentLength="200000000"
             maxNameTableCharCount="16384"/>
    </binding>
  </basicHttpBinding>
</bindings>

For TCP:

<bindings>
  <netTcpBinding>
    <binding name="tcpBinding"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="200"
           maxArrayLength="200000000"
           maxStringContentLength="200000000"
           maxBytesPerRead="4096"
           maxNameTableCharCount="16384"/>
    </binding>
  </netTcpBinding>
</bindings>

IMPORTANT:

If you try to pass complex object that has many connected objects (e.g: a tree data structure, a list that has many objects...), the communication will fail no matter how you increased the Quotas. In such cases, you must increase the containing objects count:

<behaviors>
  <serviceBehaviors>
    <behavior name="NewBehavior">
      ...
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
Cummings answered 25/7, 2017 at 9:58 Comment(1)
The maxItemsInObjectGraph was the (quick) solution for me. But when increasing this, you should think about whether the better solution is for your application to request data in chunks, as opposed to a huge object graph that may strain resources.Empurple
S
7

For me, all I had to do is add maxReceivedMessageSize="2147483647" to the client app.config. The server left untouched.

Salzburg answered 24/7, 2018 at 14:50 Comment(0)
C
6

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding_Username" maxReceivedMessageSize="20000000"          maxBufferPoolSize="20000000">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName" establishSecurityContext="false"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<client>
  <endpoint
            binding="wsHttpBinding"
            bindingConfiguration="wsHttpBinding_Username"
            contract="Exchange.Exweb.ExchangeServices.ExchangeServicesGenericProxy.ExchangeServicesType"
            name="ServicesFacadeEndpoint" />
</client>

Chemar answered 26/1, 2012 at 12:38 Comment(1)
Great for posting your answer. It is important that the "bindingConfiguration" value matches the binding name. In your example "wsHttpBinding_Username".Tchao
S
3

Don't forget that the app.config of the execution entry point will be considered, not the one in class library project managing Web-Service calls if there is one.

For example if you get the error while running unit test, you need to set up appropriate config in the testing project.

Shed answered 26/7, 2013 at 7:54 Comment(0)
C
0

i got this error when using this settings on web.config

System.ServiceModel.ServiceActivationException

i set settings like this:

      <service name="idst.Controllers.wcf.Service_Talks">
    <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_TalksAspNetAjaxBehavior"
      binding="webHttpBinding" contract="idst.Controllers.wcf.Service_Talks" />
  </service>
  <service name="idst.Controllers.wcf.Service_Project">
    <endpoint address="" behaviorConfiguration="idst.Controllers.wcf.Service_ProjectAspNetAjaxBehavior"
      binding="basicHttpBinding" bindingConfiguration="" bindingName="largBasicHttp"
      contract="idst.Controllers.wcf.Service_Project" />
  </service>
</services>

<bindings>
<basicHttpBinding>
    <binding name="largBasicHttp" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
        <readerQuotas maxDepth="32"
             maxArrayLength="200000000"
             maxStringContentLength="200000000"/>
    </binding>
</basicHttpBinding>

Chrysalis answered 22/1, 2013 at 9:53 Comment(4)
So, you fixed your problem by using Nate's answer and then posted it as your own. Not cool.Underfeed
@Underfeed Nates answer was very generic, using stock names and numbers that can be expected to come up frequently. This answer was not stolen, it is simply the right answer. As there is only one correct answer, it was bound to be repeated.Pram
@Pram The "correct" answer was already here when the answerer posted this. He pretty clearly lifted Nate's bindings element and reposted it as part of his answer. Those 2000000 values are pretty distinct.Underfeed
@arcain, I disagree those are very standard values, my company uses them in our binding elements as well.Pram

© 2022 - 2024 — McMap. All rights reserved.