WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

Today I discovered the some configuration impacts from my choice to return the XML document as a string property.  Because I am using a string property to return the XML generated by my service (see yesterday's post) I had to adjust three properties in the service and client configuration.

MaxBufferSize property - (From MSDN) Gets or sets the maximum size of the buffer to use. For buffered messages this value is the same as MaxReceivedMessageSize. For streamed messages, this value is the maximum size of the SOAP headers, which must be read in buffered mode.

I went ahead and bumped that up to about 5MB which should be plenty for the messages I'm sending. Default is 65536.

MaxReceivedMessageSize must also match what you put in the MaxBufferSize.  Default is 65536.

MaxStringContentLength - (From MSDN) Gets and sets the maximum string length returned by the reader. Default is 8192.

This one I also bumped up to 5MB.  This property actually belongs to the XMLReader that is called under the covers by WCF.  When deserializing the message this property is used to block messages over a certain size.

All three appear for several reason I'm sure but the one that jumps out is security.  See "Security Considerations for Data" for more info on what those properties can prevent.

There are configuration elements for all three of these in the new WCF config tool.

Screens:

Right click on an app.config and choose "Edit WCF Configuration"

The highlighted areas are the properties I changed.

These have been posted on several other forums, blogs, etc, but I put this out there in case someone stumbles across it.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Print | posted on Tuesday, December 11, 2007 10:34 AM

Feedback

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by prabu at 8/24/2008 9:11 PM Gravatar
how did you get the right click to "Edit WCF Configuration" ?

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Filip at 11/12/2008 4:44 AM Gravatar
In VS.NET go to tools -> WCF Configuration Manager (or something alike)

It will open the manager, you just need to close it. After that you get the "Edit WCF Configuration"

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Russell at 11/25/2008 3:23 PM Gravatar
I set all these settings, and still get the error. How do you set existing endpoints to use these settings?
I even used the tool to set the binding configuration to this new binding for the endpoint that is used, but still get the error.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Chris at 11/25/2008 9:31 PM Gravatar
You need to change the binding configuration on both the service and the client.

If your client sets this in code then you can also set it by overriding the appropriate properties. If it uses config you can pretty much just copy your binding configuration into the clients config and use that.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by rebelBodhi at 1/12/2009 2:52 PM Gravatar
Thanks, that did it. I couldn't get my service to accept large binary files (quite large, 16MB or so).. but after setting MaxReceivedMessageSize --and-- MaxBufferSize (second one did it), it works like a charm.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by erwin at 1/26/2009 11:55 AM Gravatar
Hello,

I am using a WCF Service hosted in IIS 6. I edited the WCF Service's web.config file as indicated & saved it. I also opened the web.config & ensured that the new settings were indeed there. I reset IIS, and updated the service configuration in my client app. Even now when I run my client app, I still get the error as follows-

Error in deserializing body of reply message for operation 'ServiceRequest'. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 8621.

Any help in throwing some light on this issue will be appreciated.

Thanks!

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by erwin at 1/26/2009 1:12 PM Gravatar
Hello,

Further to my previous post, I figured out the reason for the error recurring- I had made changes to a copy of the client web.config, not to the one that was actually being used. So much for debugging..!

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by SeaBuccaneer at 4/14/2009 3:13 PM Gravatar
Hi,

WORKS!!!

Thank you very much.

SeaBuccaneer

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by manish at 8/13/2009 6:10 PM Gravatar
Niem Guy, thanks so much for this blog, I had reached a dead end over this problem, your blog really helped.

Here's a question: you say in your post that
"I had to adjust three properties in the service and client configuration."
In my case, the service does not take in any large data, it only returns a large string. So, I set the properties only on the client side and it worked. Does that mean - these settings are only applicable to the receiving side of the WCF channel? No harm in leaving out these settings on the Service side if it does not receive large data?

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Andre' at 8/21/2009 10:56 AM Gravatar
Modifying the client config only seems to be sufficient. I successfuly ran some tests in VB.NET with this code which calls a test service that returns a specified number of bytes in a byte array:

Private Sub Test_GetNullBytes(ByVal ByteCount As Integer)
Using client As New TestService.TestClient
Dim request As New TestService.GetBytes_Request
Dim reply As TestService.GetBytes_Reply = Nothing
request.ByteCount = ByteCount
Console.WriteLine("Requesting {0} Null bytes.", ByteCount)

Dim binding As System.ServiceModel.NetTcpBinding = Nothing
binding = DirectCast(client.Endpoint.Binding, System.ServiceModel.NetTcpBinding)
binding.MaxReceivedMessageSize = ByteCount + 1024
binding.MaxBufferSize = ByteCount + 1024
binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max

reply = client.GetNullBytes(request)
Console.WriteLine("Received {0} Null bytes.", reply.Bytes.Length)
End Using
End Sub

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Chris (Niemguy) at 8/23/2009 11:40 AM Gravatar
The max message size affects the receiver, not the sender. In your case the client was receiving the large file, if you reversed it, say, you were going to upload files to the service then you'd need to adjust the service config.

Handy code, thanks for posting!

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Dean at 2/8/2010 2:24 PM Gravatar
Hello,
The app.config file handles the client. WHen you refer to the server.config, are you refering to the server where the client is running, or the server where the web service your client is accessing is running. I'm asking because I would have no control over the web service server.
Thanks,

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Chris at 2/9/2010 8:41 AM Gravatar
You need to change the app.config for the client and possibly the web.config on the server. It depends on which way the large message is going. The change needs to be made to the config of the receiver for the message.

Example - If the client will receive a large message you only need to change the config for the client.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by JP at 3/11/2010 11:04 PM Gravatar
Great help. Thanks.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by TheGodfather at 6/4/2010 3:43 AM Gravatar
Hi,
How can I configure service side config.
Thanks...

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by ashraf elhakim at 6/23/2010 6:17 PM Gravatar
hey
i have the same problem but i am working on mobile application and and mobile application had not app.conf how can i configre it by code .....
C# plz :)
thx for ur helpful topic

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by the same error at 6/23/2010 6:59 PM Gravatar
i tried this
System.ServiceModel.Channels.HttpTransportBindingElement httpBinding = new HttpTransportBindingElement();
httpBinding.MaxBufferSize = 10000000;
httpBinding.MaxReceivedMessageSize = 10000000;
System.ServiceModel.Channels.CustomBinding binding = new System.ServiceModel.Channels.CustomBinding();
binding.Elements.Add(new System.ServiceModel.Channels.TextMessageEncodingBindingElement(System.ServiceModel.Channels.MessageVersion.Soap11, System.Text.Encoding.UTF8));
binding.Elements.Add(httpBinding);

but still have error
There is an error in the XML document.
in this part of code
return this.serializer.Deserialize(reader);
any idea

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Ian at 8/14/2010 10:33 AM Gravatar
I am having this issue too. I have a WCF service that hosted by web server of vs2008 which called by an asp.net app. The WCF first returning a big object to the asp.net app and I got this error which I am able to fix it by adding the readerquotas settings on both client side & server side web.config.

However I got the same error again when the asp.net client tried to send the big object data back to wcf service. So any clue?

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Dev at 10/12/2010 3:31 PM Gravatar
check this out

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Shafique at 10/25/2010 3:15 PM Gravatar
Is there a performance impact from setting these values so high? Only a small percentage of messages will need VERY high values here, the rest of the messages go through just fine with the default binding settings..

# okay

left by Aprilaire Humidifier at 11/3/2010 9:11 AM Gravatar
Is there a performance impact from setting theses values too low? Most of the messages are going through but i am wondering if messing with my setting will cause any really annoying problems.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Ashvini at 11/24/2010 6:56 AM Gravatar
here i am having problem of size exceeding of xml parameter value,I have tried all the above options. but since there is error.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Nitin Goel at 3/6/2011 5:19 PM Gravatar
how about Reporting Service connection to the WCF Service as XML datasource...how can I increase these property values on the Reporting Service side..

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Sandals at 3/22/2011 4:41 AM Gravatar
Hi, it is works, Thanks a lot.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Pam at 6/23/2011 5:31 PM Gravatar
you website save me!

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Hal Diggs at 8/24/2011 1:11 PM Gravatar
thanks. I "stumbled", as you say, across it. I did find it useful.

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Sandesh at 9/7/2011 4:55 AM Gravatar
Thank you for this small but important article...very well specified

I just wanted to ask that...my service is consumed by Flash....Do I tell Flash developer to make any changes to accept large number of response ?

Thank you,
Sandesh Daddi

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

left by Mark Woodard at 2/1/2012 3:59 PM Gravatar
If MaxBufferSize, MaxReceivedMessageSize, and MaxStringContentLength all have to be the same, why doesn't the @*!%$ Config Tool change the other two when you change one of them? That could have saved me a month. (That, or better documentation.)
Fabulous article, Chris.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: