【问题标题】:WCF: The maximum array length quota (16384) has been exceeded while reading XML dataWCF:读取 XML 数据时已超出最大数组长度配额 (16384)
【发布时间】:2012-12-05 22:18:58
【问题描述】:

我知道这个问题已经被问过很多次了,相信我,我已经看了半个下午并尝试了很多,但仍然无法解决这个问题。

我正在尝试通过 Web 服务提交一些表单内容(也可以包括附件),但是当我尝试附加除小文件以外的任何内容时,总是出现以下错误。

我的完整错误是:

格式化程序在尝试反序列化 消息:尝试反序列化参数时出错 http://tempuri.org/:scChildForm。 InnerException 消息是“那里 反序列化类型对象时出错 MashForms.Models.SCChildrenForm。最大数组长度配额 读取 XML 数据时已超出 (16384)。这个配额可能是 通过更改 MaxArrayLength 属性增加 创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象。 第 1 行,位置 41713.'。有关详细信息,请参阅 InnerException。

无论我如何尝试在 WCF 服务和 Web 客户端的 web.config 中覆盖该值,它仍会将该值报告为最大错误和后续错误。

WCF Web.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
        <!--<httpRuntime maxRequestLength="409600" executionTimeout="300" />-->
  </system.web>
  <connectionStrings>
    <add name="MashFormsContext" connectionString="Data Source=ServerName;Initial Catalog=MashFormsDev;Persist Security Info=True;User ID=UserName;Password=Password" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
                    <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                    <serviceTimeouts transactionTimeout="05:05:00" />
                    <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="2147483647" />
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!--<serviceMetadata httpsGetEnabled="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" />
          <!--<StructureMapServiceBehavior />-->
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
    <extensions>
      <behaviorExtensions>
      </behaviorExtensions>
    </extensions>

        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IInsertNewForm" 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="524288" maxReceivedMessageSize="2147483647"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
          useDefaultWebProxy="true">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
            maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>

    </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我的 Web 客户端 Web.Config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-PublicForms-20121112151426;Integrated Security=SSPI" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>

    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <system.serviceModel>

    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IInsertNewForm" 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="2147483647" maxArrayLength="2147483647"
            maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:54070/MashPortalService/InsertNewForm.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IInsertNewForm"
        contract="InsertNewForm.IInsertNewForm" name="BasicHttpBinding_IInsertNewForm" />
    </client>
  </system.serviceModel>
</configuration>

我的 WCF 服务将接受非常小的文件,但不会更大,甚至 28k。

作为附加说明,当我最初将服务引用添加到 WebClient 中时,它不会默认为较大的值,它会像这样创建它:

默认 Web 客户端 Web.config

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IInsertNewForm" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" 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://localhost:54070/MashPortalService/InsertNewForm.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IInsertNewForm"
        contract="InsertNewForm.IInsertNewForm" name="BasicHttpBinding_IInsertNewForm" />
    </client>
  </system.serviceModel>

更新 1:对尤金的回应

using System.ServiceModel;
using MashForms.Models;

namespace MashPortalService
{
    [ServiceContract]
    public interface IInsertNewForm
    {
        //Testing operations
        [OperationContract]
        string TestReturnInput(string s);
        [OperationContract]
        bool Ping();

        //Child
        [OperationContract]
        int InsertFormChildGettingIdBack(SCChildrenForm scChildForm);
    }
}

更新 2:按照建议添加服务端点

</bindings>
        <services>
            <service name="MashPortalService">
                <endpoint address="http://localhost:54070/MashPortalService/InsertNewForm.svc" binding="basicHttpBinding"
                                    bindingConfiguration="BasicHttpBinding_IInsertNewForm" contract="InsertNewForm.IInsertNewForm" />
            </service>
        </services>

但我仍然有同样的错误。

【问题讨论】:

    标签: c# .net wcf web-services wcf-binding


    【解决方案1】:

    感谢所有建议。不幸的是,这些建议都没有解决这个问题,它似乎仍然从某个地方获取了一个覆盖它的值。

    我已经设法通过使用 customBinding 路线来处理更大的文件。

    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MashPortalService.MyServiceBehaviour">
                    <dataContractSerializer maxItemsInObjectGraph="2147483647"  />
                    <serviceTimeouts transactionTimeout="05:05:00" />
                    <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="2147483647" />
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!--<serviceMetadata httpsGetEnabled="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" />
                    <!--<StructureMapServiceBehavior />-->
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
        <extensions>
            <behaviorExtensions>
            </behaviorExtensions>
        </extensions>
        <bindings>
            <customBinding>
                <binding name="MashCustomBinding"
                        closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
                    <textMessageEncoding messageVersion="Soap11" maxReadPoolSize="2147483647" maxWritePoolSize="2147483647">
                        <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
                    </textMessageEncoding>
                    <httpTransport transferMode="Streamed" maxReceivedMessageSize="2147483647"/>
                </binding>
            </customBinding>
        </bindings>
        <services>
            <service behaviorConfiguration="MashPortalService.MyServiceBehaviour" name="MashPortalService.InsertNewForm">
                <endpoint address="" binding="customBinding"
                                    bindingConfiguration="MashCustomBinding" contract="MashPortalService.IInsertNewForm" />
            </service>
        </services>
    </system.serviceModel>
    

    【讨论】:

      【解决方案2】:

      它只是一个使用 wsHttpBinding 的访客。 您可以在 web.config 中编写以下代码

      <wsHttpBinding>
            <binding name="MyBinding" maxReceivedMessageSize="2147483647" />
      </wsHttpBinding>
      

      【讨论】:

        【解决方案3】:

        您已经定义了一个“BasicHttpBinding_IInsertNewForm”绑定,但您实际上是否将此绑定连接到您的服务?你在代码中这样做吗?如果是这样,请发布代码。因为我在您的配置中看不到它...通常,您将在 部分中有一个 部分,其中绑定实际上连接到您的服务。

        【讨论】:

        • 我添加了更新 1,我想这就是您的意思。感谢您的回复。 @Eugene Osovetsky 另外,正如我所提到的,它适用于
        • 不,我不是这个意思...服务如何知道它应该使用您定义的“BasicHttpBinding_IInsertNewForm”绑定?我在您发布的内容中看不到任何这样做的内容。尝试添加适当的 <services>部分到您的配置,看看它是否有帮助。在stackoverflow.com/questions/8382392/…的第一个答案中查看它是如何完成的
        • 啊,这样吗? localhost:54070/MashPortalService/InsertNewForm.svc" binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_IInsertNewForm" contract="InsertNewForm.IInsertNewForm" /> 当我尝试当我尝试从 Web 应用更新参考时它会出错。
        • 好吧,在你的情况下,它必须是 basicHttpBinding,而不是 wsHttpBinding
        • 不幸的是,这没有什么区别,它仍然抛出一个相同的错误。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-05
        • 2011-02-15
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多