Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: maxStringContentLength always set to 8192

I need to change the maxStringContentLength to a value larger than 8192 but have not been successful in doing it. My WCF service will generate an exception if the amount of data it receives is greater than 8192 bytes. I have exhausted my searches and nothing seems to help. I should point out that the exception comes from the server. Forget about the client because I am seeing the exception generated straight from WCF on the server. Here is my web.config settings:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="DevServiceBehavior" >
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="DeveloperService"
             behaviorConfiguration="DevServiceBehavior" >
      <endpoint address="mtom"
                binding="basicHttpBinding"
                bindingConfiguration="Binding_DevService"
                contract="DeveloperService">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint contract="IMetadataExchange"
                binding="mexHttpBinding"
                address="mex" />
    </service>
  </services>
  <bindings>
    <basicHttpBinding>
      <binding name="Binding_DevService"
               messageEncoding="Mtom"
               openTimeout="00:02:00"
               sendTimeout="00:02:00"
               maxBufferPoolSize ="41943040"
               maxBufferSize="2147483647"
               maxReceivedMessageSize="2147483647">
        <readerQuotas maxDepth="500"
                      maxArrayLength="20000000"
                      maxStringContentLength="20000000" />
      </binding>
    </basicHttpBinding>
  </bindings>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                              multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
like image 835
Polaris431 Avatar asked Aug 15 '11 13:08

Polaris431


2 Answers

By default, the latest version of WCF does in fact setup defaults and json is the default. What wasn't clear was what kind of default binding WCF was using. It turns out to be webHttpBinding. You will also see a ton of examples on the web showing attributes being applied to the service method, such as [WebGet]. The method requires no attributes at all. For maxStringContentLength to take affect, you need to correctly setup the binding and behavior. Here is the correct entries in the web.config file:

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="jsonBehavior">
        <enableWebScript />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="DevServiceBehavior" >
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="DeveloperService" behaviorConfiguration="DevServiceBehavior" >
      <endpoint address="" binding="webHttpBinding" contract="DeveloperService" bindingConfiguration="webHttpBindingDev" behaviorConfiguration="jsonBehavior">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
  </services>
  <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingDev">
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"  maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      </binding>
    </webHttpBinding>
  </bindings> 
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
like image 193
Polaris431 Avatar answered Oct 17 '22 20:10

Polaris431


update your client side config too. Set Reader's quota in and its attributes in the binding section.

like image 4
Tarang Avatar answered Oct 17 '22 20:10

Tarang