Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set WCF client maxBufferSize parameters?

Tags:

wcf-binding

I am setting wcf client parameters programmatically like below:

try
{
ServiceReference1.MyDbServiceClient webService =
    new ServiceReference1.MyDbServiceClient(new System.ServiceModel.BasicHttpBinding(),
    new System.ServiceModel.EndpointAddress((string)((App)Application.Current).Resources["wcfMyDBServiceEndPoint"]));

    webService.GetSeriesImagesCompleted += new EventHandler<ServiceReference1.GetSeriesImagesCompletedEventArgs>(webService_GetSeriesImagesCompleted);
    webService.GetSeriesImagesAsync(index);
}

It works just fine for the default maxBufferSize. However when a client exceeds the default size, an exception is throughn: "the maximum message size quota for incoming messages (65536) has been exceeded."

How to set this parameter in code? Thanks.

like image 432
val Avatar asked Feb 25 '26 08:02

val


1 Answers

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.CloseTimeout = new TimeSpan(00, 05, 00);
binding.OpenTimeout = new TimeSpan(00, 05, 00);
binding.ReceiveTimeout = new TimeSpan(00, 05, 00);
binding.SendTimeout = new TimeSpan(00, 05, 00);
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;             

binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, XmlDictionaryReaderQuotas.Max, null);

Create binding based on your requirement.

like image 177
hungryMind Avatar answered Feb 27 '26 01:02

hungryMind