lundi 11 mai 2015

Set ReaderQuotas.MaxStringContentLength, in WCF via reflection WSHttpBinding

i continue fighting with WCF.

I need consume WCF when i don't know the type of binding, it could be BasicHttpBinding or WSHttpBinding. I create a simple solution to test without reflection and it works. But when i try use it in other proyect via reflection when i use WSHttpBinding, i have this exception (Spanish):

{System.Xml.XmlException: Se superó la cuota de longitud del contenido de cadena (8192) al leer los datos XML. Esta cuota se puede aumentar cambiando la propiedad MaxStringContentLength en el objeto XmlDictionaryReaderQuotas que se usa para crear el lector XML. Línea 1, posición 10572.
   en System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
   en System.Xml.XmlDictionaryReader.ReadContentAsString(Int32 maxStringContentLength)
   en System.Xml.XmlBaseReader.ReadContentAsString()
   en System.Xml.XmlBaseReader.ReadElementContentAsString()
   en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.PartInfo.ReadValue(XmlDictionaryReader reader)
   en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part)
   en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeResponse(XmlDictionaryReader reader, Object[] parameters)
   en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeReply(Message message, Object[] parameters)}

in BasicHttpBinding work's Ok, (when i configure WCF with this binding)

I put in the web.config this code in WsHttpBinding server

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors >
        <behavior name="ServiceBehaviors"  >
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
          <serviceCredentials>
            <serviceCertificate findValue="localhost" x509FindType="FindBySubjectName"
                             storeLocation="LocalMachine" storeName="My" />
            <userNameAuthentication userNamePasswordValidationMode="Custom"
             customUserNamePasswordValidatorType="TestService.CustomValidator, TestService" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00"
          maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Message">
            <transport clientCredentialType="None" />
            <message clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehaviors" name="TestService.Service1">
        <endpoint binding="wsHttpBinding" contract="TestService.IService1" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

it's works with out reflection, to test this server i put in app.config client this simple code:

<bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1">
                    <readerQuotas maxStringContentLength="2147483647" />
                    <security>
                        <message clientCredentialType="None" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>

but i can't put this in my other proyect because the WCF could change. I have this code in C# to configure the dinamic web service, but it doesn't work:

            PropertyInfo channelFactoryProperty = proxyInstance.GetType().GetProperty("ChannelFactory");

            if (channelFactoryProperty == null)
            {
                throw new InvalidOperationException("There is no ''ChannelFactory'' property on the DomainClient.");
            }
            ChannelFactory factory = (ChannelFactory)channelFactoryProperty.GetValue(proxyInstance, null);

            factory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 0);
            factory.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 10, 0);
            factory.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            factory.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 10, 0);

            PropertyInfo channelFactoryPropert = proxyInstance.GetType().GetProperty("InnerChannel");
            System.ServiceModel.IClientChannel factor = (System.ServiceModel.IClientChannel)channelFactoryPropert.GetValue(proxyInstance, null);
            factor.OperationTimeout.Add(new TimeSpan(0, 10, 0));
            factor.OperationTimeout = new TimeSpan(0, 10, 0);

            switch ((factory.Endpoint.Binding).GetType().ToString())
            {
                case "System.ServiceModel.BasicHttpBinding":
                    BasicHttpBinding _basicBinding = (BasicHttpBinding)factory.Endpoint.Binding;
                    _basicBinding.MaxBufferPoolSize = 2147483647;
                    _basicBinding.MaxBufferSize = 2147483647;
                    _basicBinding.MaxReceivedMessageSize = 2147483647;
                    _basicBinding.OpenTimeout = new TimeSpan(0, 10, 0);
                    break;

                case "System.ServiceModel.WSHttpBinding":
                    WSHttpBinding _wsBinding = (WSHttpBinding)factory.Endpoint.Binding;                     
                    _wsBinding.MaxBufferPoolSize = 2147483647;
                    _wsBinding.MaxReceivedMessageSize = 2147483647;
                    _wsBinding.OpenTimeout = new TimeSpan(0, 10, 0);
                    _wsBinding.ReaderQuotas.MaxStringContentLength = 2147483647;


                    XmlDictionaryReaderQuotas _wsBindingRQ = (XmlDictionaryReaderQuotas)_wsBinding.ReaderQuotas;
                    _wsBindingRQ.MaxArrayLength = 2147483647;
                    _wsBindingRQ.MaxBytesPerRead = 2147483647;

                    _wsBindingRQ.MaxNameTableCharCount = 2147483647;
                    _wsBindingRQ.MaxStringContentLength = 2147483647;
                    break;
            } 

I don't know which code configure in C# the app.config in this proyect is empty.

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire