14

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>
Polaris431
  • 481
  • 1
  • 5
  • 14
  • Can you add the exception details? – David Hoerster Aug 15 '11 at 13:39
  • 1
    You have to configure the server and the client configuration. – Peter Aug 15 '11 at 13:52
  • 1
    What is the name of your service class (including namespaces)? – Ladislav Mrnka Aug 15 '11 at 13:53
  • If you send a smaller request (one whose quota isn't exceeded), does the server respond with a "normal" XML or with MTOM (find out in fiddler). If the former, then it's the problem Ladislav pointed out - the service name in config is incorrect (you need to use the same one as in the .svc file) – carlosfigueira Aug 15 '11 at 13:58
  • Carlosfigueira: The service works fine when the amount of data is not exceeded. – Polaris431 Aug 15 '11 at 14:10
  • Peer: That is incorrect. I am using a Java based client. A WCF service will never limit the amount of data it receives based upon what the client dictates. The only reason (Microsoft) clients do this is to make sure the client doesn't exceed the limit. That however has no effect on the value of what the server sets its limit to. The fact is, is that the server is not changing the value. I did come across another posting on stackoverflow that shows how one person hit this problem and came up with a solution by overriding the service factory. I'm trying that now. – Polaris431 Aug 15 '11 at 14:12
  • @Polaris431, what do you mean by "works fine"? Does it mean that it responds with MTOM, or with text? Even if you have a mismatch in the name, the service will *still* "work", as it will get a default endpoint (with default endpoint settings). And that would explain why you're not being able to increase the quota. – carlosfigueira Aug 15 '11 at 15:01
  • Peer: The service methods get called, receives the data, and returns data. The name of the service is correct and no you will NOT get a default endpoint if it's spelled wrong. – Polaris431 Aug 15 '11 at 15:06

2 Answers2

19

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>
Polaris431
  • 481
  • 1
  • 5
  • 14
4

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

Tarang
  • 646
  • 4
  • 6