Search code examples
wcf-binding

How to programmatically set WCF client maxBufferSize parameters?


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.


Solution

  • 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.