Geeks With Blogs
NIEM Guy - NIEM, XML, and .NET Piecing it all together

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.

Posted on Tuesday, December 11, 2007 10:34 AM | Back to top


Comments on this post: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
how did you get the right click to "Edit WCF Configuration" ?
Left by prabu on Aug 24, 2008 9:11 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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"
Left by Filip on Nov 12, 2008 4:44 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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.
Left by Russell on Nov 25, 2008 3:23 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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.
Left by Chris on Nov 25, 2008 9:31 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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.
Left by rebelBodhi on Jan 12, 2009 2:52 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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!
Left by erwin on Jan 26, 2009 11:55 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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..!
Left by erwin on Jan 26, 2009 1:12 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
Hi,

WORKS!!!

Thank you very much.

SeaBuccaneer
Left by SeaBuccaneer on Apr 14, 2009 3:13 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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?
Left by manish on Aug 13, 2009 6:10 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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
Left by Andre' on Aug 21, 2009 10:56 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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!
Left by Chris (Niemguy) on Aug 23, 2009 11:40 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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,
Left by Dean on Feb 08, 2010 2:24 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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.
Left by Chris on Feb 09, 2010 8:41 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
Great help. Thanks.

Left by JP on Mar 11, 2010 11:04 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
Hi,
How can I configure service side config.
Thanks...
Left by TheGodfather on Jun 04, 2010 3:43 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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
Left by ashraf elhakim on Jun 23, 2010 6:17 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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
Left by the same error on Jun 23, 2010 6:59 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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?
Left by Ian on Aug 14, 2010 10:33 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
check this out
Left by Dev on Oct 12, 2010 3:31 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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..
Left by Shafique on Oct 25, 2010 3:15 PM

# okay
Requesting 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.
Left by Aprilaire Humidifier on Nov 03, 2010 9:11 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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.
Left by Ashvini on Nov 24, 2010 6:56 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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..
Left by Nitin Goel on Mar 06, 2011 5:19 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
Hi, it is works, Thanks a lot.
Left by Sandals on Mar 22, 2011 4:41 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
you website save me!
Left by Pam on Jun 23, 2011 5:31 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
thanks. I "stumbled", as you say, across it. I did find it useful.
Left by Hal Diggs on Aug 24, 2011 1:11 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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
Left by Sandesh on Sep 07, 2011 4:55 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting 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.
Left by Mark Woodard on Feb 01, 2012 3:59 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
Thank you .. that worked like a charm.
Left by James on Jul 12, 2012 4:47 PM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
i have a problem while configure maxbuffersize property in wsHttpbinding. can you please clarify.
Left by Gopianand on Sep 18, 2012 1:31 AM

# re: WCF maxStringContentLength, maxBufferSize, and MaxReceivedMessageSize
Requesting Gravatar...
Congratulations !!! You save my life !
Left by Glauber Duma on Feb 19, 2013 7:50 AM

Your comment:
 (will show your gravatar)
 


Copyright © niemguy | Powered by: GeeksWithBlogs.net | Join free