views:

2062

answers:

2

Hi,

I have a dynamic client to a service. How can i change the ReaderQuotas property of it's endpoint binding?

I tried like this but it doesn't work ....

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

Even after doing this the ReaderQuotas values remain the default ones.

I also tried like this and still doesn't work:

 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;

                 }
    }

I need this so i can send more than 8kb to the service.

Need help please, and some example if possible...

Thanks, Adrya

+7  A: 

Setting quotas on a BindingElement after the binding is created has no effect on that binding.

Edit (since you don't know what binding is used):

You can use reflection to set the property. Note you should make sure the Binding actually has the property before setting it. Not all bindings have this property. If you try to set it on a binding that doesn't support it, the example will throw an 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);

Hope this helps you a bit.

Onots
+1 for mentioning these things have to be set BEFORE the client proxy and/or service host are created. Once created, they're immutable.
marc_s
Hi Marc,Thanks for the reply, but I don't know what kind of binding it is, that's why i need to do it after binding is created.Any other suggestions?Thanks, Adrya
Adrya
What do you mean, you don't know what kind of binding it is. In the ServiceHostFactory, just look in the binding and modify the quota if you need to. If what you are saying is, you don't know you need to modify the quota until after you USE the binding, then ..maybe set a flag and then restart the host (or client proxy) .
Cheeso
I edited my reply to deal with unknown bindings by using reflection.
Onots
Thank you so much, is exactly what i needed, works great :)))))
Adrya
+1  A: 

Why are you solving that in a such complex way, just alter the ReaderQuotas directly:

ie:

WSHttpBinding WebBinding = new WSHttpBinding();

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

This will work without that weird reflection sample.

Cheers

No hay Problema
Because client is dynamically created when it gets to my hands, so bindings and everything are already created, i just wanted to increase message size of that client.
Adrya