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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15654892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:29:43  来源:igfitidea点击:

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

c#.netwcfweb-configquota

提问by Pinaki Karuri

I am working with a .NET, C# application which intends to send a long XML string to a WCF Service method for further operation. When my application tries to send the XML string to WCF Service in runtime, I am getting a error message :

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

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

My application side web.config I have written the "binding" & "endpoint" as:

我的应用程序端 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" />

If any body can help me on how to solve this error I will be very much obliged. Thanks to all in advance.

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

回答by Piotr Justyna

Pinaki Karuri,

皮纳基·卡鲁里

quotas lengths depend not only on the client's configuration - they also depend on the server's one. Please post your WCF server's web.config so we can shed some light on the problem. There is a probability you already have the quota set there for 8192, so the quickest way for you would be to find and increase its value.

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

Update

更新

As far as I can see, you are missing 'readerQuotas' node from your server's web.config, so MaxStringContentLengthhas its value set to default (8192). Please refer to this link for more information: http://msdn.microsoft.com/en-us/library/system.xml.xmldictionaryreaderquotas.maxstringcontentlength.aspx

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

回答by Lingaraj

I got this error and solve by adding this - MaxItemsInObjectGraph property for the service in both the client and the server configuration .

我收到此错误并通过在客户端和服务器配置中为服务添加 this-MaxItemsInObjectGraph 属性来解决。

<dataContractSerializer maxItemsInObjectGraph="2147483647" />

server side

服务器端

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

Client side

客户端

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

and don't forget to apply this behavior to EndPoint behaviorConfiguration="endpointbehaviour"

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

回答by Rajkiran Sanchaniya

Try to set following things in bindings.

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

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

It has solved my problem. For more reference find following link http://blogfornet.com/2013/08/the-maximum-string-content-length-quota-8192-has-been-exceeded-while-reading-xml-data/

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

回答by Derek W

Here is an article on MSDN about Reader Quotas.

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

It appears that one of the reader quotas on your server side is being exceeded.

您的服务器端的其中一个读取器配额似乎已超出。

Specifically, maxStringContentLengthis being exceeded. The default value is 8192characters for maxStringContentLengthwhich as described by the error message is being exceeded.

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

But it may not be the best approach to just bump up all the values to the maximum 2147483647as some others have suggested.

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

As written in the MSDN documentation that I linked:

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

The complexity constraints provide protection from denial of service (DOS) attacks that attempt to use message complexity to tie up endpoint processing resources. Other complexity constraints include items such as a maximum element depth and a maximum length for string content within the message.

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

Coupled with the fact that you currently have Security Modeset to None- you may be setting yourself up for some problems.

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

回答by Luigi

Check that your Target Framework for the client is the same as that of the service. I had this issue and tried all of the above fixes but that did not work. Checked properties and checked the Target Framework and changed it.

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

回答by Ravi Kumar Kamboj

Client Side Binding

客户端绑定

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

Service Config File:

服务配置文件:

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