调用包含大量数据的Web服务时出错

时间:2012-10-02 03:04:19

标签: c# xml wcf

我有一个本地托管的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>

1 个答案:

答案 0 :(得分:1)

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

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

如果您使用的是.NET 4.0并且未指定service元素,那么您使用的是默认端点和默认绑定(这意味着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>

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