c# - 读取 XML 数据时已超出最大字符串内容长度配额 (8192)

标签 c# web-services wcf

我有一个 C# 4.0 控制台应用程序,它调用位于 ASP.net 4.0 网站内的 WCF Web 服务。调用 Web 服务时,出现此错误:

The formatter threw an exception while trying to deserialize the message: 
Error in deserializing body of request message for operation 'AddArticle'. 
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 60, position 267.

所以环顾四周,您似乎需要将配置文件中的 maxStringContentLength 和 maxReceivedMessageSize 属性增加到一个很大的数字。我已经这样做了,但仍然收到错误。

我的控制台应用程序的配置是:

    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IXXXInterface" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://axa-ppp/webservices/xxxinterface.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IXXXInterface" contract="XXX_YYYY_Interface.IXXXInterface" name="BasicHttpBinding_IXXXInterface"/>
    </client>

网站的配置是:

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

    <endpoint address="http://xxx-ppp/webservices/xxxinterface.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IXXXInterface" contract="XXX_YYYY_Interface.IXXXInterface" name="BasicHttpBinding_IXXXInterface"/>

编辑:

这是网站上的类文件:

public class XXXInterface : IXXXInterface
{

public bool AddArticle(string Title, string ArticleXml)
{
    ContentData article = new ContentData();
    article.Title = Title;
    article.Html = ArticleXml;
    article.FolderId = 320;

    ContentData newArticle = contentMgr.Add(article);

    if (newArticle == null)
    {
        return false;
    }
    else
    {
        return true;
    }        
}
}

最佳答案

我找到问题了! :D

问题出在 IdBookIssuerIssuedTokenBinding.CreateBindingElements() 中。

我已经重写了并且没有调用 base:

public override BindingElementCollection CreateBindingElements()
{
    BindingElementCollection elements = new BindingElementCollection();

    elements.Add(this.security);
    elements.Add(this.transport);

    return elements;
}

现在我这样做:

public override BindingElementCollection CreateBindingElements()
{
    BindingElementCollection elements = base.CreateBindingElements();

    var securityBindingElement = elements.Find<SecurityBindingElement>();
    elements.Remove(securityBindingElement);
    elements.Add(this.security);

    var transportBindingElement = elements.Find<HttpTransportBindingElement>();
    elements.Remove(transportBindingElement);
    elements.Add(this.transport);

    return elements;
}

它就像一个魅力!

我无法解释为什么我需要调用 base。如果有人能在评论中解释一下,那就太好了!

https://stackoverflow.com/a/19574170/4339857

关于c# - 读取 XML 数据时已超出最大字符串内容长度配额 (8192),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11849560/

相关文章:

java - NetworkOnMainThreadException TCP 连接 AsinkTask

c# - 对相同的 WCF 请求进行分组

web-services - Web 服务测试隔离 - 但何时验证 Web 服务本身?

java - Tomcat 在本地主机上工作正常,但在部署时出现内部服务器错误

c# - 拆分逗号分隔值 (CSV)

C# WebClient,仅支持HTML3.2

c# - 获取 "Could not establish secure channel for SSL/TLS with authority"即使 ServerCertificateValidationCallback 返回 true

c# - 无需配置的 WCF 无文件激活

c# - 具有构建操作的 VSTemplate

c# - 动态 crm 中的 AppDomain.CurrentDomain.AssemblyResolve