【问题标题】:Error when calling web service with lots of data使用大量数据调用 Web 服务时出错
【发布时间】:2012-10-02 11:39:03
【问题描述】:

我有一个本地托管的 WCF 服务。我的 web.config 如下所示:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_INavigationService" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="6553600" maxBufferPoolSize="5242880" maxReceivedMessageSize="6553600"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      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>
<client>
  <endpoint address="http://localhost/WebServices/NavigationService.svc/"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INavigationService"
    contract="NavigationService.INavigationService" name="BasicHttpBinding_INavigationService" />
</client>   
</system.serviceModel>

如果我传入少于 8K 的文本,我的服务可以正常工作。还有什么,我得到以下错误:

Sys.WebForms.PageRequestManagerServerErrorException: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'UpdateSiteMap'. 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 75, position 166

谁能告诉我如何增加文本配额?如您所见,我已将其设置为最大整数大小。

编辑

根据 Tim 的建议,这是我的新 Web.Config 文件:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="6553600" maxBufferPoolSize="5242880" maxReceivedMessageSize="6553600"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      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>
<client>
  <endpoint address="http://localhost/WebServices/NavigationService.svc/"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INavigationService"
    contract="NavigationService.INavigationService" name="BasicHttpBinding_INavigationService" />
</client>
<services>
  <service name="Navigation.INavigationService">

    <endpoint address="" 
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_INavigationService"
              contract="NavigationService.INavigationService"  />

  </service>
</services>

【问题讨论】:

  • 发布您的服务配置文件 - 看起来您需要增加那里的值,而不是在您的客户端上。
  • 您正在显示客户端的配置,您的服务器端是否也对 maxBuffer、contentLength、messageSize 等进行了相同的配置?
  • 各位,我的 Service 和我的客户端在同一个 IIS 应用程序下运行,所以两者的 web.config 是一样的。
  • Web.config 的 &lt;services&gt; 部分是什么样的?
  • @Tim -- 我认为我没有服务部分。我确实有一个 System.ServiceModel 部分

标签: c# xml wcf


【解决方案1】:

根据错误消息和您对问题的描述,您需要确保您的服务具有相同的设置,在endpoint 元素的bindingConfig 属性中引用相同的定义绑定:

<system.serviceModel>
  <services>
    <service name="Navigation.INavigationService">
      <endpoint address="" 
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_INavigationService"
                contract="NavigationService.INavigationService" />
    </service>
  </services>
</system.serviceModel>

如果您使用的是 .NET 4.0 并且没有指定服务元素,那么您使用的是默认端点和默认绑定(这意味着您的 maxStringContentLength 限制为 8192)。解决此问题的最简单方法是从配置的绑定部分中删除 name 属性 - 然后.NET 将使用您的定义作为默认绑定。例如:

<binding closeTimeout="00:01:00" ....

注意没有设置name 属性。

在这种情况下,您可能需要使用选项 1,或者创建一个没有名称的类似绑定定义,因为我不能 100% 确定客户端使用了默认绑定;他们绝对是为了服务。

有关默认绑定和端点的更多信息,请参阅 A Developer's Introduction to Windows Communication Foundation 4

基于编辑的附加答案

试试这个:

<client>
  <endpoint address="http://localhost/WebServices/NavigationService.svc/"
            binding="basicHttpBinding" 
            contract="NavigationService.INavigationService" 
            name="BasicHttpBinding_INavigationService" />
</client>

删除&lt;services&gt; 部分,因为WCF 4.0 的默认端点机制将为您处理,并从客户端端点删除bindingConfiguration 属性。

【讨论】:

  • 我删除了名称标签并添加了 标签。我需要在某处指定最大尺寸吗?
  • 只要绑定(没有名称属性)定义了更大的尺寸,你应该没问题。
  • @icemanind - 你能发布新的 Web.config 吗?我会看看并回复。
  • 尝试删除客户端和服务的 bindingConfiguration 属性 - 这可能会搞砸(老实说,我很惊讶服务甚至在引用不存在的绑定配置的情况下运行) .还要确保服务部分中的服务名称与 .svc 文件中指定的名称匹配。
  • 在 .NET 3.5 中会发生什么?它也有默认的端点机制吗?
猜你喜欢
  • 2023-04-04
  • 2012-07-13
  • 2013-09-09
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多