1

我创建了一个简单的 WCF Web 服务,它有一个方法:SubmitTicket(flightticket ft, string username, string password)

在客户端,我有一个用于填写表格(机票)并将其发送到这个新创建的 Web 服务的应用程序。当此航班机票对象超过 8192 字节时,我收到以下错误:

“反序列化飞行票类型的对象时出错。读取 XML 数据时已超出最大字符串内容长度配额 (8192)。可以通过更改创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性来增加此配额”

我在网上做了一些研究,发现我必须将 web.config(服务器)和 app.config(客户端)中的 MaxStringContentLength 设置为更高的数字。问题是,我已经通过阅读各种博客和网站尝试了两个配置文件中所有可能的设置组合,但它仍然因同样的错误而失败!

我已经附加了我的客户端和服务器配置代码(就像目前一样,它在一天中经历了许多许多变化,但没有成功)。

我注意到的一件事是,当我更新服务引用时,我的客户端应用程序上的 configuration.svcinfo 文件似乎总是显示 MaxStringContentLength 为 8192。即使我明确设置了绑定属性,它似乎也采用了所有默认值。不确定这是否与我的问题有关,但值得一提。


这是用于定义端点绑定的适用 app.config/web.config 代码:

<<<<< 客户 >>>>>

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IFlightTicketWebService" 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="65536" 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://xx.xx.xx/xxxxxxxx.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFlightTicketWebService"
            contract="FlightTicketWebService.IFlightTicketWebService"
            name="BasicHttpBinding_IFlightTicketWebService" />
    </client>
</system.serviceModel>
</configuration>

<<<<< 服务器 >>>>>

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
  <section name="GSH.FlightTicketWebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<httpRuntime maxRequestLength="16384"/>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>

<system.serviceModel>
  <bindings>
      <basicHttpBinding>
          <binding name="BasicHttpBinding_IFlightTicketWebService" 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="65536" 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>     
<serviceBehaviors>        
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="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"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
    <service name="FlightTicketWebService">
        <endpoint 
            name="FlightTicketWebServiceBinding"
            address="http://xx.xx.xx/xxxxxxxxxxx.svc" 
            binding="basicHttpBinding" 
            bindingConfiguration="BasicHttpBinding_IFlightTicketWebService"
            contract="IFlightTicketWebService"/>
    </service>
</services>
</system.serviceModel>
<system.webServer>
4

2 回答 2

4

我认为问题在于您的服务没有获取其配置,因为您已将服务名称设置为 FlightTicketWebService 而我猜实际类型位于命名空间中。使用命名空间完全限定服务名称,它应该选择您的配置

本质上,这是 WCF 4 默认端点功能的副产品,如果它没有找到匹配的配置,它将使用默认配置放置端点

于 2011-08-16T16:54:16.373 回答
2

这就是答案!我到处搜索 WCF 4.0 中这个问题的解决方案,Richard Blewett 的这篇文章是拼图的最后一块。

从我的研究中学到的主要内容:

  • 如果服务抛出异常,则只更改服务器 Web.config 文件;不要担心客户
  • 创建一个自定义basicHttpBinding
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="customBindingNameForLargeMessages">
  • 添加较大的readerQuota值(此处显示的最大可能值,根据口味调整)
        <binding name="customBindingNameForLargeMessages"
          maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647"
             maxStringContentLength="2147483647"
             maxArrayLength="2147483647"
             maxBytesPerRead="2147483647"
             maxNameTableCharCount="2147483647" />
        </binding>
    </basicHttpBinding>
</bindings>
  • 创建一个服务条目,其端点映射到自定义绑定。当端点的bindingConfiguration与绑定的名称相同时,就会发生映射:
  • 确保服务名称合同值是完全限定的 - 使用命名空间和类的名称。
<system.serviceModel>
    <services>
        <service name="Namespace.ServiceClassName">
             <endpoint 
                 address="http://urlOfYourService"
                 bindingConfiguration="customBindingNameForLargeMessages"                     
                 contract="Namespace.ServiceInterfaceName" 
                 binding="basicHttpBinding"
                 name="BasicHTTPEndpoint" />
        </service>
    </services>
于 2013-02-12T18:03:28.343 回答