以编程方式修改端点ReaderQuotas

我有一个服务的动态客户端。 如何更改其端点绑定的ReaderQuotas属性?

我试过这样但它不起作用……

DynamicProxyFactory factory = new DynamicProxyFactory(m_serviceWsdlUri); foreach (ServiceEndpoint endpoint in factory.Endpoints) { Binding binding = endpoint.Binding; binding.GetProperty(new BindingParameterCollection()).MaxArrayLength = 2147483647 binding.GetProperty(new BindingParameterCollection()).MaxBytesPerRead =2147483647; binding.GetProperty(new BindingParameterCollection()).MaxDepth = 2147483647; binding.GetProperty(new BindingParameterCollection()).MaxNameTableCharCount = 2147483647; binding.GetProperty(new BindingParameterCollection()).MaxStringContentLength = 2147483647; } 

即使这样做,ReaderQuotas值仍然是默认值。

我也尝试过这样但仍然不起作用:

  DynamicProxyFactory factory = new DynamicProxyFactory(m_serviceWsdlUri); foreach (ServiceEndpoint endpoint in factory.Endpoints) { System.ServiceModel.Channels.BindingElementCollection bec = endpoint.Binding.CreateBindingElements(); System.ServiceModel.Channels.TransportBindingElement tbe = bec.Find(); tbe.MaxReceivedMessageSize = 2147483647; tbe.MaxBufferPoolSize = 2147483647; TextMessageEncodingBindingElement textBE = bec.Find(); if (textBE != null) { textBE.ReaderQuotas.MaxStringContentLength = 2147483647; textBE.ReaderQuotas.MaxArrayLength = 2147483647; textBE.ReaderQuotas.MaxBytesPerRead = 2147483647; textBE.ReaderQuotas.MaxDepth = 2147483647; textBE.ReaderQuotas.MaxNameTableCharCount = 2147483647; } } 

我需要这个,所以我可以发送超过8kb的服务。

在创建绑定后在BindingElement上设置配额对该绑定没有影响。

编辑(因为你不知道使用了什么绑定):

您可以使用reflection来设置属性。 请注意,在设置之前,应确保Binding实际具有该属性。 并非所有绑定都具有此属性。 如果您尝试在不支持它的绑定上设置它,该示例将引发exception。

 Binding binding = endpoint.Binding; XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = _sanebutusablelimit_; myReaderQuotas.MaxArrayLength = _sanebutusablelimit_; myReaderQuotas.MaxBytesPerRead = _sanebutusablelimit_; myReaderQuotas.MaxDepth = _sanebutusablelimit_; myReaderQuotas.MaxNameTableCharCount = _sanebutusablelimit_; binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); 

希望这对你有所帮助。

为什么要以这种复杂的方式解决这个问题,只需直接改变ReaderQuotas:

即:

WSHttpBinding WebBinding = new WSHttpBinding();

WebBinding.ReaderQuotas.MaxArrayLength = int.MaxValue; WebBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue; WebBinding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;

这将没有那个奇怪的reflection样本。

干杯

另外需要注意的是,还需要更新Binding的以下属性以获得完整的解决方案:

 binding2.MaxBufferSize = 2147483647; binding2.MaxReceivedMessageSize = 2147483647; 

为了其他人的利益,这里有一个示例,它以编程方式在客户端和服务器上设置ReaderQuotas以及上面的2个属性:

客户代码:

  WebHttpBinding binding2 = new WebHttpBinding(); XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = 2147483647; myReaderQuotas.MaxArrayLength = 2147483647; myReaderQuotas.MaxBytesPerRead = 2147483647; myReaderQuotas.MaxDepth = 2147483647; myReaderQuotas.MaxNameTableCharCount = 2147483647; binding2.GetType().GetProperty("ReaderQuotas").SetValue(binding2, myReaderQuotas, null); binding2.MaxBufferSize = 2147483647; binding2.MaxReceivedMessageSize = 2147483647; ServiceEndpoint ep = new ServiceEndpoint(ContractDescription.GetContract(typeof(IMyService)), binding2, new EndpointAddress("http://localhost:9000/MyService")); WebChannelFactory cf2 = new WebChannelFactory(ep); IMyService serv = cf2.CreateChannel(); serv.PrintNameDesc("Ram", new string('a', 100*1024*1024)); 

服务器代码:

  WebHttpBinding binding2 = new WebHttpBinding(); XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = 2147483647; myReaderQuotas.MaxArrayLength = 2147483647; myReaderQuotas.MaxBytesPerRead = 2147483647; myReaderQuotas.MaxDepth = 2147483647; myReaderQuotas.MaxNameTableCharCount = 2147483647; binding2.GetType().GetProperty("ReaderQuotas").SetValue(binding2, myReaderQuotas, null); binding2.MaxBufferSize = 2147483647; binding2.MaxReceivedMessageSize = 2147483647; WebServiceHost host2 = new WebServiceHost(typeof(MyService)); host2.AddServiceEndpoint(typeof(IMyService), binding2, new Uri("http://localhost:9000/MyService")); host2.Open(); 

合同是:

 [ServiceContract] public interface IMyService { [WebInvoke(Method = "PUT", UriTemplate = "My/{name}/", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)] [OperationContract] void PrintNameDesc(string name, string desc); }