c# wcf readerquotas

c# - Modificar endpoint ReaderQuotas programáticamente



wcf (3)

¿Por qué estás resolviendo eso de una manera tan compleja, simplemente modifica directamente las ReaderQuotas:

es decir:

WSHttpBinding WebBinding = new WSHttpBinding ();

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

Esto funcionará sin esa extraña muestra de reflexión.

Aclamaciones

Tengo un cliente dinámico para un servicio. ¿Cómo puedo cambiar la propiedad ReaderQuotas de su enlace de punto final?

Lo intenté así pero no funciona ...

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

Incluso después de hacer esto, los valores de ReaderQuotas siguen siendo los predeterminados.

También lo intenté así y todavía no funciona:

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<System.ServiceModel.Channels.TransportBindingElement>(); tbe.MaxReceivedMessageSize = 2147483647; tbe.MaxBufferPoolSize = 2147483647; TextMessageEncodingBindingElement textBE = bec.Find<TextMessageEncodingBindingElement>(); if (textBE != null) { textBE.ReaderQuotas.MaxStringContentLength = 2147483647; textBE.ReaderQuotas.MaxArrayLength = 2147483647; textBE.ReaderQuotas.MaxBytesPerRead = 2147483647; textBE.ReaderQuotas.MaxDepth = 2147483647; textBE.ReaderQuotas.MaxNameTableCharCount = 2147483647; } }

Necesito esto para poder enviar más de 8kb al servicio.


Establecer cuotas en un BindingElement después de crear el enlace no tiene ningún efecto en ese enlace.

Editar (ya que no sabes qué enlace se utiliza):

Puede utilizar la reflexión para establecer la propiedad. Tenga en cuenta que debe asegurarse de que el enlace realmente tenga la propiedad antes de establecerla. No todos los enlaces tienen esta propiedad. Si intenta establecerlo en un enlace que no lo admite, el ejemplo lanzará una excepción.

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

Espero que esto te ayude un poco.


También se debe tener en cuenta que las siguientes propiedades del enlace también deben actualizarse para la solución completa:

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

Para el beneficio de los demás, aquí hay una muestra que establece de manera programática ReaderQuotas tanto en el cliente como en el servidor junto con las 2 propiedades anteriores:

Codigo del cliente:

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<IMyService> cf2 = new WebChannelFactory<IMyService>(ep); IMyService serv = cf2.CreateChannel(); serv.PrintNameDesc("Ram", new string(''a'', 100*1024*1024));

Código del servidor:

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

Donde el contrato es:

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