2009-06-09 12 views
17

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

traté como este, 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 permanecen ReaderQuotas la falta de pago.

también probé 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, así que puede enviar más de 8 kb para el servicio.

Respuesta

33

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

Editar (ya que no se sabe lo que se utiliza la unión):

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 configurarlo. No todas las vinculaciones tienen esta propiedad. Si intenta establecerlo en un enlace que no lo admite, el ejemplo generará 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.

+4

+1 por mencionar estas cosas tienen que ser establecido antes de crear el proxy de cliente y/o host de servicio. Una vez creados, son inmutables. –

+0

Hola Marc, Gracias por la respuesta, pero no sé qué tipo de enlace es, por eso tengo que hacerlo después de crear el enlace. ¿Alguna otra sugerencia? Gracias, Adrya – Adrya

+0

¿Qué quieres decir con que no sabes qué tipo de encuadernación es? En ServiceHostFactory, solo mire en el enlace y modifique la cuota si es necesario. Si lo que dices es que no sabes que debes modificar la cuota hasta después de USAR el enlace, entonces ... puedes establecer un indicador y luego reiniciar el servidor (o servidor cliente). – Cheeso

9

¿Por qué está resolviendo que en una tal manera compleja, simplemente altera las ReaderQuotas directamente:

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 muestra de reflexión extraña.

Saludos

+2

Como el cliente se crea dinámicamente cuando llega a mis manos, por lo que las vinculaciones y todo ya están creados, solo quería aumentar el tamaño del mensaje de ese cliente. – Adrya

0

También hay que señalar, es que las siguientes propiedades de la unión también necesitan ser actualizadas para la solución completa:

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

Para el beneficio de otros aquí es una muestra de que mediante programación establece los ReaderQuotas tanto en el cliente y el servidor junto con las 2 propiedades anteriores:

código 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)); 
012 código

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

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