Friday, January 15, 2010

Setting the WCF MaxBufferSize, MaxReceiveMessageSize, and MaxArrayLength in Code instead of App.config

While the concept of using the app.config file for configuring the many WCF parameters is a nice idea. I sometime like setting the values in code. The most common one for me are MaxBufferSize, MaxReceiveMessageSize, and maxArrayLength because they are usually too small by default for most of my applications. Below I showthe app.config file with these parameters set and then how you can set the same parameters with code in C# and VB.NET.


app.config











VB.NET

' Increase binding max sizes so that the image can be retrieved
If TypeOf pltSvr.Endpoint.Binding Is ServiceModel.BasicHttpBinding Then
Dim binding As ServiceModel.BasicHttpBinding = _
CType(pltSvr.Endpoint.Binding, ServiceModel.BasicHttpBinding)
Dim max As Integer = 5000000 ' around 5M size allowed
binding.MaxReceivedMessageSize = max
binding.MaxBufferSize = max
binding.ReaderQuotas.MaxArrayLength = max
End If



C#

// Increase binding max sizes so that the image can be retrieved
if (pltSvr.Endpoint.Binding is System.ServiceModel.BasicHttpBinding)
{
System.ServiceModel.BasicHttpBinding binding =
(System.ServiceModel.BasicHttpBinding)pltSvr.Endpoint.Binding;

int max = 5000000; // around 5M
binding.MaxReceivedMessageSize = max;
binding.MaxBufferSize = max;
binding.ReaderQuotas.MaxArrayLength = max;
}

3 comments:

Anonymous said...

Hi,

Nice informative post. Thanks.

Is there a way we can do this on server side (WCF service side) instead of client side?

Unknown said...

This worked perfectly for me ... the config settings are cryptic so this was a great help.

Unknown said...

This worked perfectly for me and was v helpful as the config settings are cryptic.