繁体   English   中英

将 XML 字符串发送到 WCF 时出现“读取 XML 数据时已超出最大字符串内容长度配额 (8192)”错误

[英]"The maximum string content length quota (8192) has been exceeded while reading XML data" error while sending XML string to WCF

我正在使用 .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" />

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

这是一篇关于读者配额的 MSDN文章。

似乎超出了服务器端的一个读取器配额。

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

但它可能不是像其他人所建议的那样将所有值提升到最大值2147483647的最佳方法。

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

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

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

我得到了这个错误,并通过在客户端和服务器配置中添加此服务的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”

客户端绑定

    <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>

Pinaki Karuri,

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

更新

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

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

<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/

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

服务器端

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM