如何以编程方式设置WCF客户端maxBufferSize参数?

时间:2011-01-18 18:49:18

标签: wcf-binding

我正在以编程方式设置wcf客户端参数,如下所示:

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);
}

它适用于默认的maxBufferSize。但是,当客户端超过默认大小时,会出现例外情况:“已超出传入邮件的最大邮件大小限额(65536)。”

如何在代码中设置此参数? 感谢。

1 个答案:

答案 0 :(得分:10)

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);

根据您的要求创建绑定。