18

我正在使用一个 .NET、C# 应用程序,该应用程序打算将一个长 XML 字符串发送到 WCF 服务方法以进行进一步操作。当我的应用程序尝试在运行时将 XML 字符串发送到 WCF 服务时,我收到一条错误消息:

"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:strProdUserDataXML. The InnerException message was 'There was an error deserializing the object of type System.String. 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 131, position 57.'. Please see InnerException for more details."

我的应用程序端 web.config 我已将“绑定”和“端点”编写为:

<binding name="EndPointHTTPGenericPortal" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    <security mode="None">
    <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    </binding>

    <endpoint address="http://192.168.140.40/WcfGenericPortal_Service/Service1.svc" binding="basicHttpBinding" bindingConfiguration="EndPointHTTPGenericPortal" contract="IService1" name="EndPointHTTPGenericPortal" behaviorConfiguration="Graph" />

如果任何机构可以帮助我解决这个错误,我将非常感激。提前感谢大家。

4

7 回答 7

10

这是 MSDN 上关于Reader Quotas的文章。

似乎超出了您服务器端的读者配额之一。

具体来说,超出了 maxStringContentLengthmaxStringContentLength的默认值为8192个字符,如错误消息所述,已超出此值。

但正如其他一些人所建议的那样,将所有值提高到最大值2147483647可能不是最好的方法。

正如我链接的 MSDN 文档中所写:

复杂性约束提供了对拒绝服务 (DOS) 攻击的保护,这些攻击试图使用消息复杂性来占用端点处理资源。其他复杂性约束包括消息中字符串内容的最大元素深度和最大长度等项目。

再加上您当前将安全模式设置为这一事实- 您可能会遇到一些问题。

于 2013-08-17T15:01:13.133 回答
8

我得到了这个错误并通过在客户端和服务器配置中为服务添加这个 MaxItemsInObjectGraph 属性来解决。

<dataContractSerializer maxItemsInObjectGraph="2147483647" />

服务器端

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="Service.Service1Behavior">
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
</system.serviceModel>

客户端

<behaviors >
  <endpointBehaviors>
    <behavior name="endpointbehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

并且不要忘记将此行为应用于 EndPoint behaviorConfiguration="endpointbehaviour"

于 2013-03-27T09:22:58.673 回答
7

客户端绑定

    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService11" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
messageEncoding="Text">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647"
maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>

<behaviors>
<endpointBehaviors>
<behavior name="KAMServiceDistributor">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:1234/xxxx/Service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService11"
contract="yourservice namespae" name="AnyName" />
</client>
</system.serviceModel>

服务配置文件:

<system.serviceModel>
<behaviors>


<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer ignoreExtensionDataObject="false" maxItemsInObjectGraph="2147483646" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding <b>maxReceivedMessageSize="2147483647"</b>>
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="Service">
<endpoint binding="basicHttpBinding" contract="IService" />
</service>
</services>
</system.serviceModel>
于 2014-10-30T12:57:23.043 回答
6

尝试在绑定中设置以下内容。

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

它解决了我的问题。有关更多参考,请找到以下链接http://blogfornet.com/2013/08/the-maximum-string-content-length-quota-8192-has-been-exceeded-while-reading-xml-data/

于 2013-08-17T09:56:17.180 回答
5

皮纳基·卡鲁里,

配额长度不仅取决于客户端的配置 - 它们还取决于服务器的配置。请发布您的 WCF 服务器的 web.config,以便我们对问题有所了解。有可能您已经为8192设置了配额,因此您最快的方法是找到并增加其价值。

更新

据我所知,您在服务器的 web.config 中缺少“readerQuotas”节点,因此MaxStringContentLength将其值设置为默认值(8192)。请参阅此链接了解更多信息:http: //msdn.microsoft.com/en-us/library/system.xml.xmldictionaryreaderquotas.maxstringcontentlength.aspx

于 2013-03-27T09:11:07.287 回答
0

检查客户端的目标框架是否与服务的目标框架相同。我遇到了这个问题并尝试了上述所有修复,但没有奏效。检查属性并检查目标框架并更改它。

于 2014-05-28T11:19:44.780 回答
0

服务器端

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding maxBufferPoolSize="2147483647"
                     maxReceivedMessageSize="2147483647"
                     maxBufferSize="2147483647">
                <readerQuotas maxDepth="200"
                              maxStringContentLength="2147483647"
                              maxArrayLength="16384"
                              maxBytesPerRead="2147483647"
                              maxNameTableCharCount="16384" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

客户端

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IRequestService" allowCookies="true"
        maxReceivedMessageSize="2147483647"
        maxBufferSize="2147483647"
        maxBufferPoolSize="2147483647">
                <readerQuotas maxDepth="32"
          maxArrayLength="2147483647"
          maxStringContentLength="2147483647"/>
            </binding>
            <binding name="BasicHttpBinding_IAttachmentService" allowCookies="true"
        maxReceivedMessageSize="2147483647"
        maxBufferSize="2147483647"
        maxBufferPoolSize="2147483647">
                <readerQuotas maxDepth="32"
          maxArrayLength="2147483647"
          maxStringContentLength="2147483647"/>
            </binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>
于 2021-12-30T15:08:51.753 回答