WCF在CustomBinding中为HttpTransportBindingElement修改ReaderQuotas

时间:2012-02-01 02:43:37

标签: c# .net wcf wcf-binding

BasicHttpBinding类有一个ReaderQuotas属性,您可以访问该属性来覆盖MaxArrayLengthMaxBytesPerRead等属性。

ReaderQuotas而不是HttpTransportBindingElement中使用CustomBinding时,如何访问BasicHttpBinding以实现相同的目标?

即:

var bindingElement = new HttpTransportBindingElement();
bindingElement.MaxBufferSize = 65536; // works
bindingElement.ReaderQuotas.MaxArrayLength = 65536; // error no ReaderQuotas member

var binding = new CustomBinding(bindingElements);
binding .ReaderQuotas.MaxArrayLength = 65536; // also no ReaderQuotas member

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

你可以试试下面的内容:

var binding = new CustomBinding();
var myReaderQuotas = new XmlDictionaryReaderQuotas();
myReaderQuotas.MaxStringContentLength = 5242880;
binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); 

希望有所帮助。

答案 1 :(得分:1)

您需要使用消息编码绑定元素TextMessageEncodingBindingElement而不是HttpTransportBindingElement

        var bindingElement = new TextMessageEncodingBindingElement();
        bindingElement.ReaderQuotas.MaxArrayLength = 65536;

        var binding = new CustomBinding();
        binding.Elements.Add(bindingElement);

可以使用其他message encoder types(即二进制或MTOM)但如果您正在进行直接转换the default for basicHttpBinding is text

  

WSMessageEncoding的值,指示是MTOM还是Text / XML   用于编码SOAP消息。默认值为Text。