How to increase the MaxStringContentLength size when creating the XML reader

折月煮酒 提交于 2019-12-25 05:14:38

问题


I am getting following error:

The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'InsertQuery'. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 33788.

To increase the size of MaxStringContentLength, i modified my Web.config as given below..

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- 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="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingDev">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"  maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

Even then i am getting the same error :(

Please let me know what changes need to me made in order to resolve this issue. Thanks in advance !!


回答1:


You're Web.config file indicates that you're using .NET 4.0, and there is no endpoint explicitly defined in the Web.config, so WCF is giving you a default endpoint (based on the location of your *.svc file), and using the default binding of basicHttpBinding for the http scheme. The default for maxStringContentLength is 8192.

To change that, you either need to:

  1. Add an endpoint explicitly and assign the binding you've defined (using the binding configuration's `name` attribute) to the endpoint's `bindingConfiguration` attribute, or
  2. make the binding configuration you've defined in your config file the default for that type of binding by removing the `name` attribute, and change the default mapping for `http` from `basicHttpBinding` to `webHttpBinding`.

To do it via an explicit endpoint, add the following to your Web.config file under <system.serviceModel>:

<services>
  <service name="your service name">
    <endpoint address="" binding="webHttpBinding" 
              bindingConfiguration="webHttpBindingDev" 
              contract="your fully-qualified contract name" />
  </service>
</services>

You'll have to supply the proper values for your service name and contract.

To do it via setting defaults, you'll need to mark your specified binding configuration for webHttpBinding as the default by removing the name attribute:

<bindings>
  <webHttpBinding>
    <binding>
      <readerQuotas maxDepth="2147483647" 
                    maxStringContentLength="2147483647"  
                    maxArrayLength="2147483647" 
                    maxBytesPerRead="2147483647" 
                    maxNameTableCharCount="2147483647" />
    </binding>
  </webHttpBinding>
</bindings>

Next you'll need to set webHttpBinding as the default binding for the http scheme:

<protocolMapping>
  <add binding="webHttpBinding" scheme="http" />
</protocolMapping>

With these two changes, you won't need to add an explicit endpoint.

Also, since you're using webHttpBinding, I believe you need to add the following to the endpoint behaviors:

<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

Take a look at A Developer's Introduction to Windows Communication Foundation 4 for more information on default endpoints, bindings, etc in WCF 4.



来源:https://stackoverflow.com/questions/18849341/how-to-increase-the-maxstringcontentlength-size-when-creating-the-xml-reader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!